1

I have a script that tries to run some executable using a relative path.
So I use test-path to verify that the executable is where it is supposed to be. If not I try another location.

if(test-path "$current../../../myexe.exe"){
   # found it!
}

But in this case, if the $current is C:/folder/ then test-path "C:/folder/../../../myexe.exe" fails with

The path ... referred to an item that was outside the base 'C:'

Is there a clean and sure way to test for a path, so that it returns true or false, and won't throw me some unexpected error?

4 Answers 4

2
Test-Path ([io.path]::Combine($current,(Resolve-Path ../../../myexe.exe)))

See this thread for more info:

Sign up to request clarification or add additional context in comments.

5 Comments

I just got it to work, I used [IO.File]::Exists() I think that Resolve-Path would throw the same kind of exception
I wouldn't change the process working directory, leeholmes.com/blog/2006/06/26/…
Interesting, then I should maybe use [IO.Path]::GetFullPath("$pwd\..\..\myexe.exe") to avoid the exception of resolve-path, and then File.Exists to avoir the exception of test-path
You can use Test-Path instead of the .net class.
ah that's right, because in this case, GetFullPath takes care of the problematic exception.
2

I got it to work using .NET File.Exists, but you have to set the Environment.CurrentDirectory first if you want relative path to be resolved correctly.

EDIT: not changing CurrentDirectory after Shay Levy pointed out that it can be dangerous for other background processes (see http://www.leeholmes.com/blog/2006/06/26/current-working-directory-with-powershell-and-net-calls/ )

 [Environment]::CurrentDirectory = $pwd

[System.IO.File].Exists("$pwd\$invalidRelativePath")
False

Comments

2

Test-path is fundamentally broken.

Even SilentlyContinue is broken:

Test-Path $MyPath -ErrorAction SilentlyContinue 

This will still blow up if $MyPath is $null, empty, or doesn't exist as a variable.

It even returns $true if $MyPath is only a space. Where the heck is that " " folder!

Below is a workaround that work for the following situations:

$MyPath = "C:\windows"  #Test-Path return $True as it should
$MyPath = " "       #Test-Path returns $true, Should return $False
$MyPath = ""        #Test-Path Blows up, Should return $False
$MyPath = $null      #Test-Path Blows up, Should return $False
Remove-Variable -Name MyPath -ErrorAction SilentlyContinue  #Test-Path Blows up, Should return $False

The solution lies in forcing it to return $False when Test-Path wants to blow up.

if ( $(Try { Test-Path $MyPath.trim() } Catch { $false }) ) {  #Returns $false if $null, "" or " "
    write-host "path is GOOD"
} Else {
    write-host "path is BAD"
}

Comments

0

You should use Resolve-Path or Join-Path

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.