我正在更新一个批处理文件,它最初有一个可执行文件的变量,如下所示:set var=C:\SomeProgram\program.exe
我能够使用cmd.exe的以下语法从批处理文件正确运行程序:start "" cmd /k %var% "%param1%" "%param2%"
必须在网络上移动一些东西,现在变量是这样的:set var=D:\Some Group\3. Team Number\SomeProgram\program.exe
空格和\
往往会导致问题,这取决于组合,我知道可以通过用""
这样的"%var%"
括住变量来解决。但是,当我尝试运行命令时:start "" cmd /k "%var%" "%param1%" "%param2%"
我得到:D:\Some is not recognized as an internal...yada yada...
。通过一些调试:start "" cmd /k "%var%"
运行应用程序start "" cmd /k ""%var%" "%param1%" "%param2%""
运行应用程序
为什么我现在需要将周围的""
包含到整个命令字符串中,而我以前不必这样做,因为应用程序端点位置现在用""
括起来了?
最佳答案
cmd.exe
可能非常古怪。如果键入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.
在您的情况下,治疗1的条件并不都满足,因此使用治疗2。在
cmd /k "%var%" "%param1%" "%param2%"
中,第一个和最后一个引号将被删除,以不正确的cmd /k %var%" "%param1%" "%param2%
结尾。正如您所做的那样,在/k
之后的整行添加引号可以解决问题。关于windows -/k标志之外的双引号命令字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48082829/