Skip to content
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

Open
jonkyops opened this issue Apr 17, 2017 · 3 comments
Open

File Path Formatting #84

jonkyops opened this issue Apr 17, 2017 · 3 comments

Comments

@jonkyops
Copy link

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'

$Server = 'Server1'
$Share = 'Share1'
$SubFolder = 'SubFolder1'
$File = 'File1.txt'

Join-Path

  1. Piping
    $FullPath = Join-Path -Path $Server -ChildPath $Share | -ChildPath $SubFolder | -ChildPath $File

  2. MultiLine

$Path1 = Join-Path -Path $Server -ChildPath $Share
$Path2 = Join-Path -Path $Path -ChildPath $SubFolder
$FullPath = Join-Path -Path $Path -ChildPath $File

-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)

@rkeithhill
Copy link

rkeithhill commented Apr 17, 2017

Somebody (was it @dlwyatt) modified Join-Path in PS Core to behave like Path.Combine:

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.

@Jaykul
Copy link
Member

Jaykul commented Apr 17, 2017

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 Join-Path "X:\Temp" "SubFolder" if you don't have an X: drive.

Basically nothing will fix the path separators without caring about drive names that don't exist except [IO.Path]::GetFullPath -- but even that one only works if your drive names are single letters.

What we do currently is a mishmash mix of interpolation and the -join operator passed to Join-Path to fix our slashes for us. We actually have an internal wrapper for Join-Path which uses Split-Path $Path -IsAbsolute (why is that on Split-Path, and not Test-Path?) and then rips off the drive qualifier with $Drive = Split-Path $Path -Qualifier -ErrorAction Ignore and $Path = Split-Path $Path -NoQualifier and then uses Join-Path and puts the $Drive qualifier back at the end...

@Objections
Copy link

There have been some improvements on this in powerfully 6+ with -AdditionalChildPath

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/join-path?view=powershell-6#optional-parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants