200mg
200mg

Reputation: 531

Append Directory to UNC Path

I need to append a directory to a unc path, i need the following

\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\

To look like

\\10.0.0.1\share\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\

I'm having a hard time figuring out how to add a string after the first \\ in the UNC path.

Upvotes: 0

Views: 102

Answers (2)

mklement0
mklement0

Reputation: 437111

A pragmatic solution is to do simple string concatenation, given that duplication of \ in paths is benign (multiple \ are treated as a single \; try Get-ChildItem C:\\Windows, for instance).

$uncShare = '\10.0.0.1\share'
$path = '\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch'

# Works fine.
"${uncShare}${path}"

# Ditto
Join-Path $uncShare $path

Upvotes: 0

AdminOfThings
AdminOfThings

Reputation: 25001

You can use regex replace to accomplish this:

"\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\" -replace "^\\",'\\10.0.0.1\share'

You could also use Join-Path as Olaf has suggested:

Join-Path -path "\\10.0.0.1\share" -ChildPath "\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\".TrimStart('\')

Both of the above solutions work if your child path is in a variable as well:

$Path = "\\APP401\I\Run\Folder\Client\20171031\25490175\Data\brtbvsch\"
$Path -replace "^\\",'\\10.0.0.1\share'

Or:

Join-Path -Path "\\10.0.0.1\share" -ChildPath $Path.TrimStart('\')

Upvotes: 1

Related Questions