cmd.exe Command Line Parsing of Environment Variables

Posted by Artefacto on Stack Overflow See other posts from Stack Overflow or by Artefacto
Published on 2011-01-15T13:38:21Z Indexed on 2011/01/15 13:54 UTC
Read the original article Hit count: 228

Filed under:
|
|

I can't figure how to have cmd.exe not interpret something like %PATH% as an environment variable. Given this program:

#include<stdio.h>
#include<windows.h>

int main(int argc, char *argv[])
{
    int i;

    printf("cmd line: %s\n", GetCommandLine());
    for (i = 0; i < argc; i++) {
        printf("%d: %s\n", i, argv[i]);
    }
    return 0;
}

I have these different outputs according to the position of the arguments:

>args "k\" o" "^%PATH^%"
cmd line: args  "k\" o" "%PATH%"
0: args
1: k" o
2: %PATH%

>args "^%PATH^%" "k\" o"
cmd line: args  "^%PATH^%" "k\" o"
0: args
1: ^%PATH^%
2: k" o

I guess it's because cmd.exe doesn't recognize the escaped \" and sees the escaped double quote as closing the first, leaving in the first case %PATH% unquoted. I say this, because if I don't quote the argument, it always works:

>args ^%PATH^% "k\" o"
cmd line: args  %PATH% "k\" o"
0: args
1: %PATH%
2: k" o

but then I can have no spaces...

© Stack Overflow or respective owner

Related posts about Windows

Related posts about command-line