Skip to content

File Path Formatting #84

Open
Open
@jonkyops

Description

@jonkyops

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions