Windows – How to escape spaces in command line in Windows

ccommand linepdfwindows

I have a similar problem like this How do I escape spaces in command line in Windows without using quotation marks?

But it seems I can't use the carets

C:\>C:\Program^ Files^ (x86)\Adobe\Reader^ 11.0\Reader\AcroRd32.exe
C:\Program Files is not recognized as an internal or external command, operable program or batch file.

cd works fine:

C:\>cd C:\Program^ Files^ (x86)\Adobe\Reader^ 11.0\Reader\
C:\Program Files (x86)\Adobe\Reader 11.0\Reader>

I'm calling the Adobe Reader from C++ App with

UniString cmd("\"C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe\" ");
system(cmd.ToCStr());

This is OK, Adobe reader started. But when I add another escaped quotes to pass the argument path to pdf

UniString cmd("\"C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe\" \"");
cmd.Append("A\\B with space\\C\\test.pdf");
cmd.Append("\"");
system(cmd.ToCStr());

I got

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

What is going on here? Why can't I use the carets or escaped quotes more times?

(Geez, I hate windows so much)

Best Answer

I believe you should use the CreateProcess() function rather than launching the cmd.exe program and then telling it to run acrobat.

Its more complicated to set up but you don't need to pass through cmd's admittedly odd command line handling.

update: I just took a quick look as someone else had a similar question which provides an answer which may help: https://stackoverflow.com/questions/486087/how-to-call-an-external-program-with-parameters

Related Question