Start process with args which contains a path with blanks

前端 未结 3 1109
终归单人心
终归单人心 2021-01-08 01:35

I need to start a process from a powershell script and pass such params : -a -s f1d:\\some directory\\with blanks in a path\\file.iss to do that, I write the folowing code :

3条回答
  •  有刺的猬
    2021-01-08 02:07

    I understand your question to be: How to pass multiple arguments to start a process where one of the arguments has spaces?

    I'm assuming the equivalent in a Windows batch file would be something like:

    "%setupFilePath%" -a -s -f1"d:\some directory\with blanks in a path\fileVCCS.iss"
    

    where the double quotes allow the receiving process (setupFilePath in this case) to receive three arguments:

    1. -a
    2. -s
    3. -f1"d:\some directory\with blanks in a path\fileVCCS.iss"

    To accomplish this with the code snippet in your question I would use back ticks (to the left of the 1 and below the escape key, not to be confused with a single quote; aka Grave-accent) to escape the inner double quotes like this:

    $process = [System.Diagnostics.Process]::Start("$setupFilePath", "-a -s -f1`"d:\some directory\with blanks in a path\fileVCCS.iss`"") 
    $process.WaitForExit()
    

    Note that in addition to using back ticks I also changed the single quotes around your argument list to double quotes. This was necessary because single quotes do not allow the escapes we need here (http://ss64.com/ps/syntax-esc.html).

    Aaron's answer should work just fine. If it doesn't then I would guess that setupFilePath is not interpretting -f1"d:\space here\file.ext" as you expect it to.

    OPINION ALERT The only thing I would add to his answer is to suggest using double quotes and back ticks in order to allow using a variable within the path for argument -f1:

    Start-Process -FilePath $setupFilePath -ArgumentList '-a','-s',"-f1`"$pathToVCCS`"" |
    Wait-Process
    

    This way you won't have a hard-coded, absolute path in the middle of a long line.

提交回复
热议问题