在bat脚本中使用空格,不带引号

Qia*_*iao 17 batch-file

我需要跑

start cmd.exe /k "C:\Program Files (x86)\Mozilla Firefox\sdk\bin\activate & cd d:\home\name\addon\ & cfx run"
Run Code Online (Sandbox Code Playgroud)

问题是第一个路径有空格(Program Files).我不能使用引号,因为它们已用于完整命令.

如何在此命令中使用空格?

dbe*_*ham 16

谁说在引用/ C命令时你不能在exe路径周围添加引号?它工作正常!:-)

start cmd.exe /k ""C:\Program Files (x86)\Mozilla Firefox\sdk\bin\activate" & cd d:\home\name\addon\ & cfx run"
Run Code Online (Sandbox Code Playgroud)

/ C命令周围的外引号在执行之前被删除,只留下exe路径周围的引号.您可以在帮助中阅读有关CMD流程报价的方式.只需键入CMD /?HELP CMD从命令提示符.它确实令人困惑.

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.
Run Code Online (Sandbox Code Playgroud)

您的命令有两个以上的引号,因此遵循选项2.

如果你的exe名称包含一个特殊的字符&,例如,上面这段时间不起作用的唯一时间this&that.exe.这会导致问题,因为最初解析START命令时不会引用exe名称.可以通过转义文件名中的问题字符来解决这个问题.

start cmd.exe /k ""this^&that.exe" & echo something else"
Run Code Online (Sandbox Code Playgroud)