How do I escape spaces in command line in Windows?

06
2014-04
  • bartimar

    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)

  • Answers
  • Ian

    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: http://stackoverflow.com/questions/486087/how-to-call-an-external-program-with-parameters

  • bartimar

    I solved it:

    for system() I had to use two escaped quotes

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

    or better with WinExec()

    UniString cmd("\"C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe\" \"D:\\Second Path\\Foo\\Bar\");
    WinExec(cmd.ToCStr(),SW_SHOW);
    
  • KdgDev

    Enclose the entire string with double quotes, like so: "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe"


  • Related Question

    How to combine wildcards and spaces (quotes) in an Windows command?
  • Jan Fabry

    I want to remove directories of the following format:

    C:\Program Files\FogBugz\Plugins\cache\[email protected]_NN
    

    NN is a number, so I want to use a wildcard (this is part of a post-build step in Visual Studio). The problem is that I need to combine quotes around the path name (for the space in Program Files) with a wildcard to match the end of the path. I already found out that rd is the remove command that accepts wildcards, but where do I put the quotes? I have tried no ending quote (works for dir), ...example.com*", ...example.com"*, ...example.com_??", ...cache\"[email protected]*, ...cache"\[email protected]*, but none of them work.

    (How many commands to remove a file/directory are there in Windows anyway? And why do they all differ in capabilities?)


  • Related Answers
  • grawity

    rmdir does not support wildcards. It only accepts complete filenames.

    You can try this alternative:

    for /d %f in ("C:\Program Files\FogBugz\Plugins\cache\[email protected]_*") do rmdir /s/q "%~f"
    

    (The /s/q arguments to rmdir do the same thing as rm -rf on Unix. The for /d argument makes for match directory names instead of file names.)


    Remember that the cmd.exe shell does not do wildcard expansion (unlike Unix sh) -- this is handled by the command itself.

  • Darth Android

    You can escape the space with the ^ character: C:\Program^ Files\FogBugz\Plugins\cache\[email protected]_*