in reply to Re: Re: Re: Windows command line
in thread Windows command line

The documented way that double quotes work (on a Microsoft command-line to a C program) is that inside of double quotes, special characters aren't special except that \" becomes ", \\\" becomes \", and \\" becomes \ followed by the closing " (etc.) -- sequences of \s not immediately followed by a " are not changed.

It appears to me that Microsoft made some stab at also supporting "" inside of double quotes becoming " but that they have a few bugs in this code. Since I didn't find this behavior documented, I just avoid it and use \" when I want a " inside of double quotes and things are well behaved (as far as I've noticed) -- though the scheme leaves much to be desired.

                - tye

Replies are listed 'Best First'.
Re: Re^4: Windows command line (use \")
by BrowserUk (Patriarch) on Oct 22, 2003 at 23:31 UTC

    Agreed backslashing embedded double quotes is the easiest way of dealing with things on the command line. However, I'd still like to get a grip on exactly how double-quotes are parsed by CMD.EXE.

    One reason is that if you ever have to try and pass a filename that contains spaces to an external program via cmd.exe, especialy if it is one that won't accept forward slashes as path separators, the mess of backslashed backslashes escaping embedded quotes is just so damned messy to get right.

    I'm sure that there is some logic to it, even if it is twisted logic. I think the source of the messiness related to the fact that you can quote individual parts of a complete path as well as a whole path.

    P:\test>dir d:\"Program Files"\"Apache Group"\* Volume in drive D is Winnt Volume Serial Number is D822-5AE5 Directory of d:\Program Files\Apache Group 31/05/02 17:38 <DIR> . 31/05/02 17:38 <DIR> .. 31/05/02 17:38 <DIR> Apache2 3 File(s) 0 bytes 947,634,176 bytes free

    However, that's notth complete story as if you try and pass this into a c program like perl

    P:\test>perl pq.pl8 d:\"Program Files"\"Textpad 4"\* (d:"Program) (Files"Textpad 4\*)

    You get an almighty mess. Add an extra set of quotes and you get

    P:\test>perl pq.pl8 "d:\"Program Files"\"Textpad 4"\*" (d:"Program Files"Textpad) (4\*)

    TO achieve the desired result you have to escape the backslashes

    P:\test>perl pq.pl8 d:\\"Program Files"\\"Textpad 4"\* (d:\Program Files\Textpad 4\*)

    I can't help but think that using backslash as an escape character on a system that has programs that require the backslash be used as the path separator is a terminally (sic) bad idea.

    Oh well. Our's is not to reason why......


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!

      However, I'd still like to get a grip on exactly how double-quotes are parsed by CMD.EXE.

      Well, I'm mostly talking about how " are dealt with by the C RTL, for which it isn't that hard to obtain the source code, so just go read the code. How CMD.EXE deals with them isn't as important. It affects whether special characters cause you problems, but it has no impact on how arguments are split up, etc. (Unix defines the interface to programs to be a list of argument strings while Win32 defines the interface as a single command-line string.)

                      - tye