我正在更新一个最初有可执行变量的批处理文件,如下所示:
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%""
运行应用程序
为什么我现在需要将周围的""
包含在我之前不需要的整个命令字符串中,因为应用程序端点位置现在包含在""
中?
答案 0 :(得分:4)
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
之后在整行上添加引号可以解决问题。