Nilzor
Nilzor

Reputation: 18573

How I resolve the absolute path of a relative or absolute file name input parameter in a powershell script?

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

Upvotes: 1

Views: 664

Answers (1)

marsze
marsze

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

Related Questions