![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||