Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmd works but C:\Windows\System32\cmd.exe does not

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 to cmd.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.

like image 901
Ganesh Satpute Avatar asked Mar 11 '26 09:03

Ganesh Satpute


2 Answers

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"

like image 98
Sazid Avatar answered Mar 14 '26 05:03

Sazid


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
like image 22
MC ND Avatar answered Mar 14 '26 07:03

MC ND