how to escape " in cmd.exe /c parameters?

I'm trying to run a command from Perl, using Windows 7's CMD /C. The command, when run from the prompt, works fine, but needs quoting for its parameter:

C:\>"C:\Program Files (x86)\gs\uninstgs.exe" "C:\Program Files (x86)\gs\gs8.63\uninstal.txt"

Without the quotes, it does not work.

If I try to run this via CMD /C, I found no way to coerce CMD.EXE to pass a quoted string as parameter to the exe file. These do not work:

C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" "C:\Program Files (x86)\gs\gs8.63\uninstal.txt"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe C:\Program Files (x86)\gs\gs8.63\uninstal.txt"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" \"C:\Program Files (x86)\gs\gs8.63\uninstal.txt\"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" "\"C:\Program Files (x86)\gs\gs8.63\uninstal.txt\""
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" ""C:\Program Files (x86)\gs\gs8.63\uninstal.txt""
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" """C:\Program Files (x86)\gs\gs8.63\uninstal.txt"""
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" ^"C:\Program Files (x86)\gs\gs8.63\uninstal.txt^"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

C:\>C:\Windows\System32\cmd.exe /C "C:\Program Files (x86)\gs\uninstgs.exe" "^"C:\Program Files (x86)\gs\gs8.63\uninstal.txt^""
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

What syntax should I use?


Funny that cmd.exe actually contains the answer.

Here's a snipped from 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.  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 in your case it would be:

C:\>C:\Windows\System32\cmd.exe /C ""C:\Program Files (x86)\gs\uninstgs.exe" "C:\Program Files (x86)\gs\gs8.63\uninstal.txt""

That said, it would also be possible to use the 8.3 short names and thus truncate Program Files to Progra~1 or Progra~2. Even more, you could use relative paths and first navigate to c:\Program Files(x86) before executing your command. Your command would then become:

C:\>cd /d "c:\Program Files (x86)" && C:\Windows\System32\cmd.exe /C ".\gs\uninstgs.exe .\gs\gs8.63\uninstal.txt"