• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

find in files and replace

D

Dan Holmes

#1
i am trying to replace values in files. I get this "in use" message. Is this because the gc command still has the file
open? How do others do this?

PS D:\Sandbox> dir *.sql | %{$fn = $_.name ;gc $_.name } | % {$_ -replace "SharedAgencyID", "CoordinatedAgencyID" }|sc
$fn -Force
Get-Content : The process cannot access the file 'D:\Sandbox\DM73427 - Coordination trips and
billing\TripCoordinationListOffered.sql' because it is being used
by another process.
At line:1 char:33
+ dir *.sql | %{$fn = $_.name ;gc <<<< $_.name } | % {$_ -replace "SharedAgencyID", "CoordinatedAgencyID" }|sc $fn -Force
+ CategoryInfo : ReadError: (D:\Sandbox\DM73...ListOffered.sql:String) [Get-Content], IOException
+ FullyQualifiedErrorId : GetContentReaderIOError,Microsoft.PowerShell.Commands.GetContentCommand
 
D

Dan Holmes

#2
On 3/25/2010 2:13 PM, Dan Holmes wrote:

> i am trying to replace values in files. I get this "in use" message.
> Is this because the gc command still has the file open? How do others do
> this?
>
> PS D:\Sandbox> dir *.sql | %{$fn = $_.name ;gc $_.name } | % {$_
> -replace "SharedAgencyID", "CoordinatedAgencyID" }|sc $fn -Force
> Get-Content : The process cannot access the file 'D:\Sandbox\DM73427 -
> Coordination trips and billing\TripCoordinationListOffered.sql' because
> it is being used
> by another process.
> At line:1 char:33
> + dir *.sql | %{$fn = $_.name ;gc <<<< $_.name } | % {$_ -replace
> "SharedAgencyID", "CoordinatedAgencyID" }|sc $fn -Force
> + CategoryInfo : ReadError: (D:\Sandbox\DM73...ListOffered.sql:String)
> [Get-Content], IOException
> + FullyQualifiedErrorId :
> GetContentReaderIOError,Microsoft.PowerShell.Commands.GetContentCommand
Ok, it was powershell that had the file open. I exited that session and started a new one. When i issued the command
this time.

PS D:\Sandbox\DM73427 - Coordination trips and billing> dir *.sql | %{$fn = $_.fullname ;gc $_.fullname } | % {$_
-replace "SharedAgencyID", "CoordinatedAgencyID";} | sc $fn -force
Set-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:115
+ dir *.sql | %{$fn = $_.fullname ;gc $_.fullname } | % {$_ -replace "SharedAgencyID", "CoordinatedAgencyID";} | sc
<<<< $fn -force
+ CategoryInfo : InvalidData: (:) [Set-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetContentCommand

Every subsequent time creates the error above.
 
D

Dan Holmes

#3
On 3/25/2010 3:06 PM, Dan Holmes wrote:

> On 3/25/2010 2:13 PM, Dan Holmes wrote:

>> i am trying to replace values in files. I get this "in use" message.
>> Is this because the gc command still has the file open? How do others do
>> this?
>>
>> PS D:\Sandbox> dir *.sql | %{$fn = $_.name ;gc $_.name } | % {$_
>> -replace "SharedAgencyID", "CoordinatedAgencyID" }|sc $fn -Force
>> Get-Content : The process cannot access the file 'D:\Sandbox\DM73427 -
>> Coordination trips and billing\TripCoordinationListOffered.sql' because
>> it is being used
>> by another process.
>> At line:1 char:33
>> + dir *.sql | %{$fn = $_.name ;gc <<<< $_.name } | % {$_ -replace
>> "SharedAgencyID", "CoordinatedAgencyID" }|sc $fn -Force
>> + CategoryInfo : ReadError: (D:\Sandbox\DM73...ListOffered.sql:String)
>> [Get-Content], IOException
>> + FullyQualifiedErrorId :
>> GetContentReaderIOError,Microsoft.PowerShell.Commands.GetContentCommand
>
> Ok, it was powershell that had the file open. I exited that session and
> started a new one. When i issued the command this time.
>
> PS D:\Sandbox\DM73427 - Coordination trips and billing> dir *.sql |
> %{$fn = $_.fullname ;gc $_.fullname } | % {$_ -replace "SharedAgencyID",
> "CoordinatedAgencyID";} | sc $fn -force
> Set-Content : Cannot bind argument to parameter 'Path' because it is null.
> At line:1 char:115
> + dir *.sql | %{$fn = $_.fullname ;gc $_.fullname } | % {$_ -replace
> "SharedAgencyID", "CoordinatedAgencyID";} | sc <<<< $fn -force
> + CategoryInfo : InvalidData: (:) [Set-Content],
> ParameterBindingValidationException
> + FullyQualifiedErrorId :
> ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetContentCommand
>
>
> Every subsequent time creates the error above.
i found a better way. In the above i had the braces in the wrong place. The set-content should have been inside the
the main pipeline not a part of it. I am still a bit bothered by the fact the file was still considered open on the error.

Select-String -Path *.sql SharedAgencyID -list |
%{
(gc $_.path ) |
% {$_ -replace "SharedAgencyID","CoordinatedAgencyID"} |
sc $_.path
}