Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

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

Reply
 
Old 05-19-2006   #1 (permalink)
Andrew Watt [MVP]


 
 

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

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
Old 05-19-2006   #2 (permalink)
Andrew Watt [MVP]


 
 

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

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
Old 05-19-2006   #3 (permalink)
Kevin Loo [MSFT]


 
 

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

This does seem like a bug. I will file it internally. Thanks.
--
Kevin Loo [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>
???????: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
Old 05-23-2006   #4 (permalink)
Lee Holmes [MSFT]


 
 

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

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
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
Reply

Thread Tools


Similar Threads
Thread Forum
Set-Content not updating file after get-content and forEach-Object PowerShell
EMC and Microsoft Form New Enterprise Content Management Alliance, Extend Microsoft Office SharePoint Server With Content, Compliance and Archive Solutions Vista News
Issue: getting/setting variable content using Get/Set-Content PowerShell
About adding content to the same file using "add-content" PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46