Reputation: 18573
I'm creating a script that produces an output file which I want the user to specify the name of. The name can be input as relative or absolute path. When relative it's relative the current directory
Given this pseudo code...:
function global:FileProduce() {
Param([Parameter] [String] $dest)
$absoluteDest=(DoSomething $dest)
}
..when run from c:\temp
I want the following input to produce the following output ($absoluteDest
):
Case 1:
Input:
$dest=foo.txt
Output:
c:\temp\foo.txt
Case 2:
Input:
$dest=sub\bar.txt
Output:
c:\temp\sub\bar.txt
Case 3:
Input:
$dest=c:\foo.txt
Output:
c:\foo.txt
Resolve-Path
but it fails if the file doesn't already exist.$PSScriptRoot
doesn't help because I want the path to be relative to the current folder, not the folder of the scriptGet-Location
is fine for resolving the current directory but I don't know if the input path is relative or absolute, so (Get-Location + $dest)
will fail for absolute paths.Upvotes: 1
Views: 664
Reputation: 17055
.NET's Path.Combine will achieve exactly that:
[System.IO.Path]::Combine($pwd, $dest)
($pwd
is an automatic variable containing the full path of the current directory)
Upvotes: 1