3

I've got a string that will be dynamically generated from a 3rd party application

$somePath = "D:\some\path\name.of - my file [20_32_21].mp4"

I need to be able to verify this path in a function.

$somePath = "D:\some\path\name.of - my file [20_32_21].mp4"

Function ValidatePath{
    Param($path)
    if(Test-Path $path){
        Write-Host "Worked"
    } else {
        Write-Host "Didn't Work"
    }
}

ValidatePath $somePath 
# DIDN'T WORK

The problem is that it fails on the square brackets.

How can I automatically escape the square brackets in order to validate the file?

# Path needs to look like this
$somePath = "D:\some\path\name.of - my file ``[20_32_21``].mp4"
ValidatePath $somePath 
# WORKED!!!

2 Answers 2

5

Use -LiteralPath instead of -Path; e.g.:

if ( test-path -literalpath $path ) {
  ....
}

Bill

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

1 Comment

Simple and beautiful. Some of the semantics of PS seem elusive until you actually see it. I didn't know about that bit.
0

Can you try using "test-path -literalpath $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.