View Single Post
Old 05-23-2006   #5 (permalink)
Marcel J. Ortiz [MSFT]


 
 

Re: Weirdness with get-content | replace | set-content - file content is deleted!!

> 2) Load the entire content of the file into memory, and then make your
> changes into the original file.


Here's a quick way to edit your command so that it does just that:

(get-content NewUsers.txt) | foreach-object {$_ -replace "NAM\\", "ABC\"} |
set-content NewUsers.txt

Putting parentheses around the get-content forces it to finish reading all
the content before streaming starts. Of course, this does mean that your
whole file is being loaded into memory, so... avoid that for large files.

--
Marcel Ortiz [MSFT]
Windows PowerShell
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.



"Lee Holmes [MSFT]" <lee.holmes@online.microsoft.com> wrote in message
news:OMR6wIofGHA.3468@TK2MSFTNGP03.phx.gbl...
> This is because we stream lines from the file one-by-one. When you try to
> set the content of the file after processing the first line, we still have
> it open so that we can send you the second line.
>
> If you want to change a file, you'll have to do one of two things:
> 1) Stream your change to a temporary file, and then swap the two
> 2) Load the entire content of the file into memory, and then make your
> changes into the original file.
>
> --
> Lee Holmes [MSFT]
> Windows PowerShell Development
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
>
> "Andrew Watt [MVP]" <SVGDeveloper@aol.com> wrote in message
> news:dfmr625litssqlitrvt2nqngnpv0471a21@4ax.com...
>> Using set-content to send the modified content to a different file is
>> no problem.
>>
>> I also did an extra get-content NewUsers.txt after the error message.
>> The content has already gone.
>>
>> Andrew Watt MVP
>>
>>
>> On Fri, 19 May 2006 15:37:37 +0100, "Andrew Watt [MVP]"
>> <SVGDeveloper@aol.com> wrote:
>>
>>>This has now happened 6 or 7 times with the same file.
>>>
>>>As part of something else that I was doing I wanted to do a replace
>>>operation on some text in a text file.
>>>
>>>Here is the text copied from the screen.
>>>
>>>PS C:\PowerShellScripts> get-content NewUsers.txt
>>>newuser,NAM\aw789,New Registered Users/
>>>fullname,NAM\aw789,"Smith, John [CIR]"
>>>setrole,NAM\aw789,User
>>>
>>>newuser,NAM\sm902,New Registered Users/
>>>fullname,NAM\sm902,"Adams, Penny [SOB]"
>>>setrole,NAM\sm902,User
>>>
>>>newuser,NAM\wb008,New Registered Users/
>>>fullname,NAM\wb008,"Algar, Camilla [GM]"
>>>setrole,NAM\wb008,User
>>>PS C:\PowerShellScripts> get-content NewUsers.txt | foreach-object {$_
>>>-replace "NAM\\", "ABC\"} | set-content NewUsers.txt
>>>Set-Content : The process cannot access the file
>>>'C:\PowerShellScripts\NewUsers
>>>.txt' because it is being used by another process.
>>>At line:1 char:86
>>>+ get-content NewUsers.txt | foreach-object {$_ -replace "NAM\\",
>>>"ABC\"} | set
>>>-content <<<< NewUsers.txt
>>>PS C:\PowerShellScripts> get-content NewUsers.txt | foreach-object {$_
>>>-replace "NAM\\", "ABC\"} | set-content NewUsers.txt
>>>PS C:\PowerShellScripts> get-content NewUsers.txt
>>>PS C:\PowerShellScripts>
>>>
>>>Notice the content of NewUsers.txt at the beginning. And at the end
>>>it's gone.
>>>
>>>The first time I try the replace I consistently get an error about the
>>>file being in use.
>>>
>>>The second time I run it, then check the content of the file it's been
>>>wiped.
>>>
>>>It's either a bug or something that I'm doing wrong. I can't see that
>>>anything that I am doing would either lock the source file, nor wipe
>>>it. Am I missing some "Doh!" content in my code?
>>>
>>>The whole content of the source file is shown. If someone could try to
>>>repro this I would be grateful.
>>>
>>>I've tried it with another source file. Same result.
>>>
>>>Thanks.
>>>
>>>Andrew Watt MVP

>
>



My System SpecsSystem Spec