-
Notifications
You must be signed in to change notification settings - Fork 288
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
File Path Formatting #84
Comments
Somebody (was it @dlwyatt) modified Join-Path Server1 Share SubFolder File
Server1/Share/SubFolder/File Would love to see this backported to Windows PowerShell. BTW while the string interpolation appeals to me because it is readable/obvious, it doesn't handle the scenario of different path dir separator chars e.g. \ vs /. Join-Path does this for you. And while PowerShell generally doesn't care which dir sep char you use, native utilities typically aren't so forgiving. |
On the up-side: Join-Path fixes path separators for you. The good news about this is that you can combine it with any of the other methods to normalize all the slashes. On the down-side: it does this by resolving the Provider for the path to determine what the separator should be, and therefore does not work with rooted paths if the path doesn't exist on the local system. That is, you can't do Basically nothing will fix the path separators without caring about drive names that don't exist except What we do currently is a mishmash mix of interpolation and the |
There have been some improvements on this in powerfully 6+ with -AdditionalChildPath |
Is there a best practice way of handling file path formatting? These are the ways I know how to format them right now, assuming a path of '\Server1\Share1\SubFolder1\File1.txt'
Join-Path
Piping
$FullPath = Join-Path -Path $Server -ChildPath $Share | -ChildPath $SubFolder | -ChildPath $File
MultiLine
-Join parameter
$FullPath = '\', $Server, $Share, $SubFolder, $File -join '\'
String interpolation, adding an underscore after the file name to show variable expansion, I know it's not valid.
$FullPath = "\\$Server\$Share\$SubFolder\$($File)_"
String formatting
$FullPath = '\\{0}\{1}\{2}\{3}' -f $Server, $Share, $SubFolder, $File
String concatenation
$FullPath = '\\' + $Server + '\' + $Share + '\' + $SubFolder + '\' + $File
.NET class
[io.path]::combine("\\$Server", $Share, $SubFolder, $File)
The text was updated successfully, but these errors were encountered: