Windows Vista Forums

find in files and replace

  1. #1


    Dan Holmes Guest

    find in files and replace

    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

      My System SpecsSystem Spec

  2. #2


    Dan Holmes Guest

    Re: find in files and replace

    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.

      My System SpecsSystem Spec

  3. #3


    Dan Holmes Guest

    Re: find in files and replace

    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
    }

      My System SpecsSystem Spec


find in files and replace
Similar Threads
Thread Forum
Reverse Find and Replace ? PowerShell
find/replace on file VB Script
find and replace a specific string in multiple files VB Script
Find and Replace Utility ? Vista General
Find/Replace PowerShell