New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Out-File should create missing parent folders #13553
Comments
Adding more detail. To me this is what I would expect to happen with the -Force flag. This could possible be classified as a bug, although I labeled it as an enhancement to be safe. Current Behavior
Desired Behavior
|
While desired-state functionality is a wonderful thing, the problem is that (a) users have come to expect It's not as concise, but you can use 'foo' | Out-File "$(New-Item -Force -Type Directory logs)/error-log.txt" |
I can understand where overloading the functionality of the Force flag could be problematic. I propose adding a new flag to achieve this functionality. While the statement you provided above works, it hurts readability. I feel like it's reasonable for the end user to expect that Out-File has the ability to create the file (and parent directories) that it's trying to write to. I also feel like this would desired functionality by end users. Of course this is just my opinion, but if others agree, I think this would be a valuable feature to add. |
Understood, but at this point I suggest you close this issue, and create a new one, using the "Feature Request/Idea" template, in which you can:
|
This thread was created by selecting Feature Request/Idea. My initial comment recommended the Force flag since it matches the functionality of New-Item. I'm not familiar enough with this repository to feel comfortable recommending anything more specific than the desire for Out-File to be capable of generating parent folders. I'm open to suggestions, and I'd happily create a new topic to propose that. |
Sorry - I missed that you had indeed used the right template - it was @iSazonov who removed the My sense was that the |
Ok, thank you, I will try that. Do you have any suggestion what the flag should be named? I'm not aware or anything with similar functionality to take cues from. |
@Joe-Zer0, good question re name for the switch (flag) - I don't think there is a precedent (except in Perhaps that's an indication that the functionality doesn't really belong there, which is my personal sense. But I don't want to discourage you from creating a proposal - feel free to create it and see what others say. |
But yeah, personally I'm kind of feeling this doesn't belong in |
@vexx32, That is, if you want However, you bring up a good point: with # Replace any existing logs/error-log.txt file
# *or*
# create the /logs/ subdir. on demand an create the 'error-log.txt' file in it.
New-Item -Force 'logs/error-log.txt' -Value 'example error' |
I was previously using "if Test-Path" prior to my output redirection. Since this doesn't seem to be getting much traction, I decided to adapt some sample code that @mklement0 posted. It's a bit cleaner that having extra if statements. I also switched back to the redirection operator since I had no use for any Out-File flags.
This is a fairly clean method of redirecting output while ensuring the destination file exist. Thanks for both of your help and feedback! |
@Joe-Zer0, that's a nice idiom, thanks for sharing. You can even make it (ever so slightly) shorter by using |
@mklement0 |
Summary of the new feature/enhancement
It would be nice for Out-File to create missing parent folders. Possibly require the -Force flag for this to take effect.
Currently to safely output to a file in a different folder, it's safest to preface your command with a New-Item -Force command.
Proposed technical implementation details (optional)
The text was updated successfully, but these errors were encountered: