You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can not overwrite files in the pipeline. Is it by design? #9782
When I tried to read a file using a pipeline, edit the contents, and save it, I got unexpected behavior.
Is this correct behavior by design ?
# Create a text file for testing
PS>'A'|Out-File-FilePath C:\test.txt
# Read file , modified, overwrite
PS>Get-Content-Path C:\test.txt -Raw |ForEach-Object {$_+'B'} |Out-File-FilePath C:\test.txt
# I expect that the output is 'AB', but it outputs nothing
PS>Get-Content-Path C:\test.txt -Raw
Yeah this is what you can call by design. Because Get-Content passes items along the pipeline one at a time, it's not going to be done reading the file by the time the Out-File cmdlet is called. In fact, the way the cmdlet is designed, the file handle isn't released until the pipeline is completed, so you can't read and write to a file in the same pipeline.
The short solution is to force all the input to be pulled before sending it along the pipe; this can be done pretty simply with some parentheses:
Given the behaviour of -Raw I don't see why it wouldn't be able to release the file handle early in that particular situation, since it's reading the whole file in one go anyway. That would be a possible feature enhancement, I would think. 🙂
Rain Sallow (/u/ta11ow) (@vexx32) This is a little off topic but this is a great example of why it'd be nice if commands were disposed directly after EndProcessing. You could place an Out-String before Set-Content and the whole thing would just work.
Ilya (@iSazonov) a true fix is pretty dicey but some of the changes necessary for #9900 will make this much less tricky to handle, as the file handle should be closed as soon as the command is done with its output.
I'll have to see if that actually is the case or there needs to be changes in Get-Content specifically. I'll open a tracking issue. 👍
Activity
vexx32 commentedon May 31, 2019
Yeah this is what you can call by design. Because
Get-Contentpasses items along the pipeline one at a time, it's not going to be done reading the file by the time theOut-Filecmdlet is called. In fact, the way the cmdlet is designed, the file handle isn't released until the pipeline is completed, so you can't read and write to a file in the same pipeline.The short solution is to force all the input to be pulled before sending it along the pipe; this can be done pretty simply with some parentheses:
Given the behaviour of
-RawI don't see why it wouldn't be able to release the file handle early in that particular situation, since it's reading the whole file in one go anyway. That would be a possible feature enhancement, I would think. 🙂SeeminglyScience commentedon May 31, 2019
Rain Sallow (/u/ta11ow) (@vexx32) This is a little off topic but this is a great example of why it'd be nice if commands were disposed directly after
EndProcessing. You could place anOut-StringbeforeSet-Contentand the whole thing would just work.vexx32 commentedon May 31, 2019
Patrick Meinecke (@SeeminglyScience) Good point! I guess this will be a bit easier moving forward. I have a couple PRs to put in first, though. 😄
mkht commentedon May 31, 2019
Thanks Rain Sallow (/u/ta11ow) (@vexx32)
I understood about the behavior. I appreciate your good solution. 😄
May I close this issue ?
vexx32 commentedon May 31, 2019
Of course! 😄
iSazonov commentedon Jun 28, 2019
Rain Sallow (/u/ta11ow) (@vexx32) Please open new tracking issue if you plan to pull a fix.
vexx32 commentedon Jun 28, 2019
Ilya (@iSazonov) a true fix is pretty dicey but some of the changes necessary for #9900 will make this much less tricky to handle, as the file handle should be closed as soon as the command is done with its output.
I'll have to see if that actually is the case or there needs to be changes in
Get-Contentspecifically. I'll open a tracking issue. 👍