find in files and replace


  1.    25 Mar 2010 #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.    25 Mar 2010 #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.    25 Mar 2010 #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 ?
Hi, I'm wanting to Reverse find and replace a particular character in a Powershell string. e.g. reverse find "b" in "abcabc" and replace with...
PowerShell
find/replace on file
Hello, I'm new to vbscript. I want to open a text file find 2 paragraph makrs and replace them with some text. When I run this code everything...
VB Script
find and replace a specific string in multiple files
basically, i have a vbscript which does a find and replace on single file, below i've shared the script code. ' replace.vbs Find Replacewith File...
VB Script
Advanced find and replace using VBScript
Howdy, I posted this in another section and was recommended to make this post in the VBScript section. Since I can't find any programs to do...
VB Script
Find & Replace in MSSQL Tables through PowerShell
Greetings, I am (very) new to PowerShell and I'm trying to absorb all it has to offer. :) Some of my colleagues have written a script to search...
PowerShell
Find and Replace Utility ?
Hello, I need a Find and Replace utility that works across multiple files. I found many such utilities, yet I need it to ask me, if I want to...
Vista General
Find/Replace
Hello, I am just learning PowerShell and need a little guidance. What I am trying to do is to search a directory recursively for files that...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 20:50.
    .