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 - About adding content to the same file using "add-content"

Reply
 
Old 08-13-2006   #1 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=


 
 

About adding content to the same file using "add-content"

I was playing around with Add-Content and ended up trying to append the same
data(a string, passed thru pipeline) to the same file.

But instead of appending the string twice in the file, the string was added
to the file only once as the following scenario shows.

# create a test file to add string to.
[^_^]PS[56]>"this is a test file`n`n" | Out-File -Encoding ASCII .\test.txt
# Now "test.txt" displays "this is a test file" with two additional newline
characters(for readability)
[^_^]PS[57]>cat test.txt
this is a test file


[^_^]PS[58]>Add-Content .\test.txt -Value "THE END" -PassThru | Add-Content
..\test.txt
[^_^]PS[59]>cat C:\programming\ps\ExtendedExamples\AddContent\test.txt
this is a test file


THE END
[^_^]PS[60]>



In the 58th statement, I have tried to aadd "THE END" to the "test.txt" file
and then sent the data to the next pipeline to append the string to the
additional file(but it's the same file as the one to which the string was
already added).


Instead of appending the string, "THE END" twice, the string was added to
the content(test.txt) only once.

Is this an expected behavior? If it is so, I am not sure what I am missing
here.
Any help would be appreciated.

====================
Sung M Kim

Please don''t bother me with spam...

My System SpecsSystem Spec
Old 08-14-2006   #2 (permalink)
=?Utf-8?B?L1wvXG9cL1wvIFtNVlBd?=


 
 

RE: About adding content to the same file using "add-content"

the first command has the file still open I think using () forcing the
command first to compleet does work :

MowPS>del test.txt
MowPS>Add-Content .\test.txt -Value "THE END" -PassThru | Add-Content
..\test.txt
MowPS>gc test.txt
THE END
MowPS>del test.txt
MowPS>(Add-Content .\test.txt -Value "THE END" -PassThru) | Add-Content
..\test.txt
MowPS>gc test.txt
THE END
THE END

Greetings /\/\o\/\/


"dance2die" wrote:

> I was playing around with Add-Content and ended up trying to append the same
> data(a string, passed thru pipeline) to the same file.
>
> But instead of appending the string twice in the file, the string was added
> to the file only once as the following scenario shows.
>
> # create a test file to add string to.
> [^_^]PS[56]>"this is a test file`n`n" | Out-File -Encoding ASCII .\test.txt
> # Now "test.txt" displays "this is a test file" with two additional newline
> characters(for readability)
> [^_^]PS[57]>cat test.txt
> this is a test file
>
>
> [^_^]PS[58]>Add-Content .\test.txt -Value "THE END" -PassThru | Add-Content
> .\test.txt
> [^_^]PS[59]>cat C:\programming\ps\ExtendedExamples\AddContent\test.txt
> this is a test file
>
>
> THE END
> [^_^]PS[60]>
>
>
>
> In the 58th statement, I have tried to aadd "THE END" to the "test.txt" file
> and then sent the data to the next pipeline to append the string to the
> additional file(but it's the same file as the one to which the string was
> already added).
>
>
> Instead of appending the string, "THE END" twice, the string was added to
> the content(test.txt) only once.
>
> Is this an expected behavior? If it is so, I am not sure what I am missing
> here.
> Any help would be appreciated.
>
> ====================
> Sung M Kim
>
> Please don''t bother me with spam...

My System SpecsSystem Spec
Old 08-14-2006   #3 (permalink)
=?Utf-8?B?ZGFuY2UyZGll?=


 
 

RE: About adding content to the same file using "add-content"

"/\/\o\/\/ [MVP]" wrote:

> the first command has the file still open I think using () forcing the
> command first to compleet does work :


Thank you for the help there, mow.
I just haven't thought about the fact that, first "add-content" would have
the lock on the file.
It seems like whenever one's working on the same file on the same
pipeline(or in a one-liner),
it'd be a good practice to actually force the command to complete using
parenthesis.

This seems like the same case when Keith Hill asked for a oneliner on how to
open a file, manipulate it through pipeline and then write to the very same
file in one line.
====================
Sung M Kim

Please don''t bother me with spam...

My System SpecsSystem Spec
Old 08-14-2006   #4 (permalink)
=?Utf-8?B?L1wvXG9cL1wvIFtNVlBd?=


 
 

RE: About adding content to the same file using "add-content"

> This seems like the same case when Keith Hill asked for a oneliner on how to
> open a file, manipulate it through pipeline and then write to the very same
> file in one line.


Yep, thats why I did think about it and tried that also in this case ;-)

"dance2die" wrote:

> "/\/\o\/\/ [MVP]" wrote:
>
> > the first command has the file still open I think using () forcing the
> > command first to compleet does work :

>
> Thank you for the help there, mow.
> I just haven't thought about the fact that, first "add-content" would have
> the lock on the file.
> It seems like whenever one's working on the same file on the same
> pipeline(or in a one-liner),
> it'd be a good practice to actually force the command to complete using
> parenthesis.
>
> This seems like the same case when Keith Hill asked for a oneliner on how to
> open a file, manipulate it through pipeline and then write to the very same
> file in one line.
> ====================
> Sung M Kim
>
> Please don''t bother me with spam...
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Different behavior of "Get-Content -Wait" on Windows Server 2008 PowerShell
how to make the email content text indexed by "Windows Desktop Search 4.0" ? Live Mail
Media Center - "restricted content - display driver" error Vista General
Weirdness with get-content | replace | set-content - file content is deleted!! 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