Problem with quotes around file names in Windows command shell

6

1

I have Windows XP Home SP3 running. Trying to run this command:

cmd /c "C:\Program Files\TortoiseSVN\bin\subwcrev.exe" .. ..\Modules\getbuildinfo.c Win32-temp-Debug\getbuildinfo2.c

It works fine (ignore the exact program and file names). However, when quoting the last argument, I get an error:

cmd /c "C:\Program Files\TortoiseSVN\bin\subwcrev.exe" .. ..\Modules\getbuildinfo.c "Win32-temp-Debug\getbuildinfo2.c"

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

When run without cmd /c but directly, both command lines run without errors. I used the cmd /c to debug a similar problem I had with system calls from a C program.

What is wrong with cmd here?

Eli Bendersky

Posted 2011-01-28T08:08:51.687

Reputation: 594

Answers

8

This is what help cmd says about quoting:

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.

So double the first and last quotes and it should work:

cmd /c ""C:\Program Files\TortoiseSVN\bin\subwcrev.exe" .. ..\Modules\getbuildinfo.c "Win32-temp-Debug\getbuildinfo2.c""

Paused until further notice.

Posted 2011-01-28T08:08:51.687

Reputation: 86 075

This applies to system as well, so don't forget the extra pair of quotes around the entire command!

– Rufflewind – 2015-03-03T02:00:56.360