cmd 有效,但 C:\Windows\System32\cmd.exe 无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23366381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cmd works but C:\Windows\System32\cmd.exe does not
提问by Ganesh Satpute
I am trying to invoke one executable by putting following line on command prompt. (I know I can directly invoke the exe but let's just say I have no other way to do this due to some restriction)
我试图通过在命令提示符下放置以下行来调用一个可执行文件。(我知道我可以直接调用 exe,但我只能说由于某些限制我没有其他方法可以做到这一点)
"cmd /C" "C:\Program Files\ABC\xyz.exe" -register="abc"
itself It is successfully run. /C
is parameter to cmd.exe
. But when I do this
本身就运行成功了。/C
是 的参数cmd.exe
。但是当我这样做时
"C:\Windows\System32\cmd.exe /C" "C:\Program Files\ABC\xyz.exe" -register="abc"
Gives me error
给我错误
The directory name is invalid
Any idea why? And how can I solve this problem? I have to use full path of cmd.exe.
知道为什么吗?我该如何解决这个问题?我必须使用 cmd.exe 的完整路径。
采纳答案by Ganesh Satpute
Strange it seems :/
看起来很奇怪:/
C:\Windows\System32\cmd.exe /C " "C:\Program Files\ABC\xyz.exe" -register="abc" "
This works. Don't know why. May be the double quotes before and after "C:\\Program Files\ABC\xyz.exe" -register="abc"
are required. Wish if someone will explain that.
这有效。不知道为什么。可能需要前后双引号"C:\\Program Files\ABC\xyz.exe" -register="abc"
。希望有人能解释一下。
回答by Sazid
Try this instead:
试试这个:
"C:\Windows\System32\cmd.exe" /C " "C:\Program Files\ABC\xyz.exe" -register="abc" "
For example:
例如:
"C:\Windows\System32\cmd.exe" /C " echo "Hello World" "
"C:\Windows\System32\cmd.exe" /C " python -c " print 'Hello World' "
These work without any problem and both of them output "Hello World"
这些工作没有任何问题,它们都输出“Hello World”
回答by MC ND
As stated by Stephan, the correct way of writing it is some of the following options
正如斯蒂芬所说,正确的写作方式是以下一些选项
"C:\Windows\System32\cmd.exe" /C ....
"%comspec%" /c ....
The question is Why "cmd /c" .... works?
It works for the way the parser is interpreting the line.
问题是Why "cmd /c" .... works?
它适用于解析器解释该行的方式。
When the line is readed and parsed, "cmd /c"
is converted to
当该行被读取和解析时,"cmd /c"
被转换为
execute the command interpreter with the /c" ... arguments
So it is executed as
所以它被执行为
%comspec% /c ".....
This substitution can be easily tested
这种替代很容易测试
set "ComSpec=c:\windows\system32\calc.exe"
"cmd /c" echo hello
回答by Harry Johnston
Regarding the additional question of why the extra quotes are needed: this is described in the help returned by cmd /?
, specifically
关于为什么需要额外引号的附加问题:这在返回的帮助中进行了描述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. [Special case, not relevant here] 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.
如果指定了 /C 或 /K,则切换后的命令行的其余部分将作为命令行处理,其中使用以下逻辑处理引号 (") 字符:
1. [Special case, not relevant here] 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.
So, if the first (non-whitespace) character of the command is a quote, you need an extra pair of quotes around the entire command.
因此,如果命令的第一个(非空白)字符是引号,则整个命令周围需要一对额外的引号。
Additional note: combining MC ND's answer with mine, the first command line in the question is being interpreted like this: we start with
附加说明:将 MC ND 的答案与我的答案相结合,问题中的第一个命令行被解释如下:我们从
"cmd /C" "C:\Program Files\ABC\xyz.exe" -register="abc"
which becomes
变成
%ComSpec% /C" "C:\Program Files\ABC\xyz.exe" -register="abc"
due to the rule that replaces cmd
-> %ComSpec%
combined with the bug/feature that discards the extra quote mark; this then becomes
由于替换cmd
->的规则%ComSpec%
与丢弃额外引号的错误/功能相结合;这然后变成
"C:\Program Files\ABC\xyz.exe" -register="abc
because of the rule that removes the first and last quote marks when processing /C
.
因为处理时删除第一个和最后一个引号的规则/C
。
The Win32 file system rules discard the extra backslash, so the executable launched is
Win32 文件系统规则丢弃额外的反斜杠,因此启动的可执行文件是
C:\Program Files\ABC\xyz.exe
and the executable is presumably ignoring the missing close-quote in its argument.
并且可执行文件可能忽略了其参数中缺少的关闭引号。
回答by Stephan
"C:\Windows\System32\cmd.exe /C"
looks for a file named C:\Windows\System32\cmd.exe /C
.
查找名为C:\Windows\System32\cmd.exe /C
.
Have you ever seen a file with the extension .exe /c
?
你见过带有扩展名的文件.exe /c
吗?
Correct format is:
正确格式为:
"C:\Windows\System32\cmd.exe" /C