cmd works but C:\Windows\System32\cmd.exe does not
Solution 1
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.
Solution 2
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"
Solution 3
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.
When the line is readed and parsed, "cmd /c"
is converted to
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
Solution 4
"C:\Windows\System32\cmd.exe /C"
looks for a file named C:\Windows\System32\cmd.exe /C
.
Have you ever seen a file with the extension .exe /c
?
Correct format is:
"C:\Windows\System32\cmd.exe" /C
Solution 5
Regarding the additional question of why the extra quotes are needed: this is described in the help returned by cmd /?
, specifically
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.
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
"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
"C:\\Program Files\ABC\xyz.exe" -register="abc
because of the rule that removes the first and last quote marks when processing /C
.
The Win32 file system rules discard the extra backslash, so the executable launched is
C:\Program Files\ABC\xyz.exe
and the executable is presumably ignoring the missing close-quote in its argument.
Ganesh Satpute
Computer Software professional. Enthusiast in programming, Java and Hadoop
Updated on May 29, 2020Comments
-
Ganesh Satpute almost 4 years
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)
"cmd /C" "C:\\Program Files\ABC\xyz.exe" -register="abc"
itself It is successfully run.
/C
is parameter tocmd.exe
. But when I do this"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.
-
Alex K. about 10 years+1 or avoiding hard coded paths;
"%COMSPEC%" /C "C:\Prog ..."
-
Sazid about 10 years@GaneshS OK, now I get what you were trying to do. You should have been much more clear about your question. But, there you go, the edited answer :D
-
Ganesh Satpute about 10 yearsIf that the case, any idea "cmd /C" "C:\\Program Files\ABC\xyz.exe" -register="abc" why this works??
-
Ganesh Satpute about 10 yearsAny idea why this works? As per my interpretation " "abc" "xyz" " should be intrepreted as wrong format not as nested double quotes.
-
Stephan about 10 years
"cmd /c"
and"cmd.exe"
works,"cmd.exe /c"
doesn't. Seems to use different parsers. Let's wait for one of our experts to answer... -
Stephan about 10 yearsso
cmd
is different to (for example)notepad
.notepad
searches in the path for a filenotepad.xxx
, wherexxx
isbat
,cmd
orexe
(something, that is in%pathext%
), whilecmd
is not searching forcmd.exe
but seems to be a sort of "internal command", which executes%comspec%
, maybe like adoskey
macro? -
MC ND about 10 years@Stephan, yes, it is different. I don't know if it is related, but
start
command does something similar. From its helpWhen you run a command that contains a the string "CMD" as the first token without an extension or path qualifier, "CMD" is replaced with the value of the COMSPEC variable. This prevents users from picking up cmd from the current directory.
-
Harry Johnston about 10 yearsIt appears that the initial quote mark is discarded as a (presumably unintentional) side-effect of the conversion of
cmd
to%ComSpec%
. -
Ganesh Satpute about 10 yearsGreat help here. Thank you very much. :)