Frage

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.

War es hilfreich?

Lösung 5

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.

Andere Tipps

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"

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
"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 

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top