Example 8.1b: Bed Bath & Beyond\ Parameter Start with the parameter you want (parameter includes spaces) Bed Bath & Beyond\ 1. Search for any " no " in parameter 2. Escape with ^ all special characters: ^ < > | & ( ) "



i.e. replace ^ with ^^

replace < with ^<

replace > with ^>

replace | with ^|

replace & with ^&

replace ( with ^(

replace ) with ^)

replace " with ^"



Bed Bath ^& Beyond\

↑ 3. double the number of trailing \ Bed Bath ^& Beyond\\

↑↑ 4. Add a leading and trailing ^" ^"Bed Bath ^& Beyond\\^"

↑↑ ↑↑ 5. If this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: ^"Bed Bath ^& Beyond\\^" Now examine how this parameter gets parsed: A. cmd.exe treats ^ as escape character and removes them.

cmd.exe finds no double quoted parts because all " are escaped with ^ ^"Bed Bath & Beyond\\^"

↓ ↓ B. cmd.exe passes this as parameter to the C/C++ program "Bed Bath & Beyond\\" C. C/C++ program sees the first double quote as beginning a double quoted part.

This double quote is not added to the parameter. ┌──────────────

Bed Bath & Beyond\\" D. C/C++ program copies characters to the output parameter.

When it encounters the \ it finds two \ followed by a "

• Two is an even number

• We are currently in a double quoted part

• The character following the " is not another "

→ Therefore two/2=1 backslashes are added to the parameter

→ and the " ends the double quoted part ┌──────────────────┐

Bed Bath & Beyond\

↑ E. Result is the desired parameter we started with Bed Bath & Beyond\

TestParams8_1b.bat

ShowParams.exe ^"Bed Bath ^& Beyond\\^"



(Note: Since this is a batch file, if there were any % we would need to double them.)



Now examine what would happen if we had only one backshash at the end instead of two: A. cmd.exe treats ^ as escape character and removes them.

cmd.exe finds no double quoted parts because all " are escaped with ^ ^"Bed Bath & Beyond\^"

↓ ↓ B. cmd.exe passes this as parameter to the C/C++ program "Bed Bath & Beyond\" C. C/C++ program sees the first double quote as beginning a double quoted part ┌──────────────

Bed Bath & Beyond\" D. C/C++ program copies characters to the output parameter.

When it encounters the \ it finds one \ followed by a "

• One is an odd number

→ One/2=0 so no backslashes are added to the parameter

• We are currently in a double quoted part

→ Therefore the " is added to the parameter. The double quoted part continues. ┌────────────────────

Bed Bath & Beyond"

↑ E. If there was more following this it would be considered a part of the same parameter.

If the command line abruptly ends, the parameter is accepted as is,

even though the double quoted part wasn't closed properly. Bed Bath & Beyond"

Example 8.1c: Command Line to C/C++ Program



Example 8.1c: Bed Bath \\\ Beyond Parameter Start with the parameter you want (parameter includes spaces) Bed Bath \\\ Beyond 1. Search for any " no " in parameter 2. Escape with ^ all special characters: ^ < > | & ( ) "



i.e. replace ^ with ^^

replace < with ^<

replace > with ^>

replace | with ^|

replace & with ^&

replace ( with ^(

replace ) with ^)

replace " with ^"



no special characters in parameter 3. double the number of trailing \ no trailing \ 4. Add a leading and trailing ^" ^"Bed Bath \\\ Beyond^"

↑↑ ↑↑ 5. If this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: ^"Bed Bath \\\ Beyond^" Now examine how this parameter gets parsed: A. cmd.exe treats ^ as escape character and removes them.

cmd.exe finds no double quoted parts because all " are escaped with ^ ^"Bed Bath \\\ Beyond^"

↓ ↓ B. cmd.exe passes this as parameter to the C/C++ program "Bed Bath \\\ Beyond" C. C/C++ program sees the first double quote as beginning a double quoted part.

This double quote is not added to the parameter. ┌────────

Bed Bath \\\ Beyond" D. C/C++ program copies characters to the output parameter.

Parser comes to the \\\ followed by a space

→ Since \\\ is not followed by a " they are copied as is. ┌────────────

Bed Bath \\\ Beyond" E. Parser comes to the "

→ We are in a double quoted part, so parser checks to see if

there's another " immediately following, which there is not,

so this " ends the double quoted part and the " is not added to the parameter.

┌───────────────────┐

Bed Bath \\\ Beyond"

↓ E. Result is the desired parameter we started with Bed Bath \\\ Beyond



Sample Test File: TestParams8_1c.bat

ShowParams.exe ^"Bed Bath \\\ Beyond^"



(Note: Since this is a batch file, if there were any % we would need to double them.)







Example 8.2: Bed "Bath" & Beyond Parameter Start with the parameter you want (parameter includes spaces) Bed "Bath" & Beyond 1. Search for any "

Double the number of \ immediately preceding any " (there are none)

Then add one more \ before each " Bed \"Bath\" & Beyond

↑ ↑ 2. Escape with ^ all special characters: ^ < > | & ( ) " i.e. replace ^ with ^^

replace < with ^<

replace > with ^>

replace | with ^|

replace & with ^&

replace ( with ^(

replace ) with ^)

replace " with ^" Bed \^"Bath\^" ^& Beyond

↑ ↑ ↑ 3. double the number of trailing \ no trailing \ 4. Add a leading and trailing ^" ^"Bed \^"Bath\^" ^& Beyond^"

↑↑ ↑↑ 5. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: ^"Bed \^"Bath\^" ^& Beyond^" Now examine how this parameter gets parsed: A. cmd.exe treats ^ as escape character and removes them.

cmd.exe finds no double quoted parts because all " are escaped with ^ ^"Bed \^"Bath\^" ^& Beyond^"

↓ ↓ ↓ ↓ ↓ B. cmd.exe passes this as parameter to the C/C++ program "Bed \"Bath\" & Beyond" C. C/C++ program sees this as a double quoted parameter.

Note that all " inside the parameter are escaped with \ ┌─────────────────────┐

"Bed \"Bath\" & Beyond"

↓ ↓ D. C/C++ program applies the rule 2n+1 backslashes followed by a "

produce n backslashes + a literal quotation mark.

\" becomes " (for n=0) Bed \"Bath\" & Beyond

↓ ↓ E. Result is the desired parameter we started with Bed "Bath" & Beyond



Sample Test File: TestParams8_2.bat

ShowParams.exe ^"Bed \^"Bath\^" ^& Beyond^"



(Note: Since this is a batch file, if there were any % we would need to double them.)





Example 8.3: Bed Bath & \"Beyond" Parameter Start with the parameter you want (parameter includes spaces) Bed Bath & \"Beyond" 1. Search for any "

Double the number of \ immediately preceding any "

Then add one more \ before each " Bed Bath & \\\"Beyond\"

↑↑↑ ↑ 2. Escape with ^ all special characters: ^ < > | & ( ) " i.e. replace ^ with ^^

replace < with ^<

replace > with ^>

replace | with ^|

replace & with ^&

replace ( with ^(

replace ) with ^)

replace " with ^" Bed Bath ^& \\\^"Beyond\^"

↑ ↑ ↑ 3. double the number of trailing \ no trailing \ 4. Add a leading and trailing ^" ^"Bed Bath ^& \\\^"Beyond\^"^"

↑↑ ↑↑ 5. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: ^"Bed Bath ^& \\\^"Beyond\^"^" Now examine how this parameter gets parsed: A. cmd.exe treats ^ as escape character and removes them.

cmd.exe finds no double quoted parts because all " are escaped with ^ ^"Bed Bath ^& \\\^"Beyond\^"^"

↓ ↓ ↓ ↓ ↓ B. cmd.exe passes this as parameter to the C/C++ program "Bed Bath & \\\"Beyond\"" C. C/C++ program sees this as a double quoted parameter.

Note that all " inside the parameter are escaped with \ ┌───────────────────────┐

"Bed Bath & \\\"Beyond\""

↓ ↓ D. C/C++ program applies the rule 2n+1 backslashes followed by a "

produce n backslashes + a literal quotation mark

\\\" becomes \"

\" becomes " Bed Bath & \\\"Beyond\"

---- -- E. Result is the desired parameter we started with Bed Bath & \"Beyond"



Sample Test File: TestParams8_3.bat

ShowParams.exe ^"Bed Bath ^& \\\^"Beyond\^"^"



(Note: Since this is a batch file, if there were any % we would need to double them.)





Example 8.4: (Bed) <Bath> & "Beyond" | \"Kohl^s Parameter Start with the parameter you want (parameter includes spaces) (Bed) <Bath> & "Beyond" | \"Kohl^s 1. Search for any "

Double the number of \ immediately preceding any "

Then add one more \ before each " (Bed) <Bath> & \"Beyond\" | \\\"Kohl^s

↑ ↑ ↑↑↑ 2. Escape with ^ all special characters: ^ < > | & ( ) " i.e. replace ^ with ^^

replace < with ^<

replace > with ^>

replace | with ^|

replace & with ^&

replace ( with ^(

replace ) with ^)

replace " with ^" ^(Bed^) ^<Bath^> ^& \^"Beyond\^" ^| \\\^"Kohl^^s

↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 3. double the number of trailing \ no trailing \ 4. Add a leading and trailing ^" ^"^(Bed^) ^<Bath^> ^& \^"Beyond\^" ^| \\\^"Kohl^^s^"

↑↑ ↑↑ 5. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: ^"^(Bed^) ^<Bath^> ^& \^"Beyond\^" ^| \\\^"Kohl^^s^" Now examine how this parameter gets parsed: A. cmd.exe treats ^ as escape character and removes them.

cmd.exe finds no double quoted parts because all " are escaped with ^ ^"^(Bed^) ^<Bath^> ^& \^"Beyond\^" ^| \\\^"Kohl^^s^"

↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ B. cmd.exe passes this as parameter to the C/C++ program "(Bed) <Bath> & \"Beyond\" | \\\"Kohl^s" C. C/C++ program sees this as a double quoted parameter.

Note all " inside the double quoted parameter are escaped with \ ┌──────────────────────────────────────┐

"(Bed) <Bath> & \"Beyond\" | \\\"Kohl^s"

↓ ↓ D. C/C++ program applies the rule 2n+1 backslashes followed by a "

produce n backslashes + a literal quotation mark

\" becomes "

\\\" becomes \" (Bed) <Bath> & \"Beyond\" | \\\"Kohl^s

-- -- ---- E. Result is the desired parameter we started with (Bed) <Bath> & "Beyond" | \"Kohl^s



Sample Test File: TestParams8_4.bat

ShowParams.exe ^"^(Bed^) ^<Bath^> ^& \^"Beyond\^" ^| \\\^"Kohl^^s^"



(Note: Since this is a batch file, if there were any % we would need to double them.)





Example 8.5: &<>^|()@!" Parameter Start with the parameter you want

(parameter includes a space) &<>^|()@!" 1. Search for any "

Double the number of \ immediately preceding any "

Then add one more \ before each " &<>^|()@!\"

↑ 2. Escape with ^ all special characters: ^ < >| & ( ) " i.e. replace ^ with ^^

replace < with ^<

replace > with ^>

replace | with ^|

replace & with ^&

replace ( with ^(

replace ) with ^)

replace " with ^" ^&^<^>^^^|^(^)^@^!\^"

↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 3. double the number of trailing \ no trailing \ 4. Add a leading and trailing ^" ^"^&^<^>^^^|^(^)^@^!\^"^"

↑↑ ↑↑ 5. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: ^"^&^<^>^^^|^(^)^@^!\^"^" Now examine how this parameter gets parsed: A. cmd.exe treats ^ as escape character and removes them.

cmd.exe finds no double quoted parts because all " are escaped with ^ ^"^&^<^>^^^|^(^)^@^!\^"^"

↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ B. cmd.exe passes this as parameter to the C/C++ program "&<>^|()@!\"" C. C/C++ program sees this as a double quoted parameter.

Note all " inside the double quoted parameter are escaped with \ ┌───────────┐

"&<>^|()@!\""

↓ ↓ D. C/C++ program applies the rule 2n+1 backslashes followed by a "

produce n backslashes + a literal quotation mark

\" becomes " &<>^|()@!\"

-- E. Result is the desired parameter we started with &<>^|()@!" Sample Test File: TestParams8_5.bat

ShowParams.exe ^"^&^<^>^^^|^(^)^@^!\^"^"



(Note: Since this is a batch file, if there were any % we would need to double them.)







The following older examples show the harder way of figuring out what to use as parameters.

(Note there are multiple ways of achieving the same result.)

Example 8.6: Command Line to C/C++ Program

Example 8.6a: &<>^|()@ ! Parameter Start with the parameter you want

(parameter includes a space) &<>^|()@ ! 1. Apply the Microsoft C/C++ parsing rules a. replace: literal " with \"

literal \" with \\\"

literal \\" with \\\\\" nothing to replace b. enclose the whole parameter in double quotes

(because there's a space in the parameter) ┌──────────┐

"&<>^|()@ !"

↑ ↑ 2. Apply the Command Prompt parsing rules (cmd.exe) a. determine what cmd.exe will see as the quoted parts ┌──────────┐

"&<>^|()@ !" b. escape the special characters not in double quoted parts:

( the escape character for cmd.exe is ^ ) Nothing to escape because it's all in a double quoted part

(as seen by cmd.exe) c. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: "&<>^|()@ !" Example 8.6b: &<>^|()@ ! Parameter Start with the parameter you want

(same as example 1a) &<>^|()@ ! 1. Apply the Microsoft C/C++ parsing rules a. replace: literal " with \"

literal \" with \\\"

literal \\" with \\\\\" nothing to replace b. enclose spaces in double quotes

(A double quoted part can be anywhere within a parameter) &<>^|()@" "!

↑ ↑ 2. Apply the Command Prompt parsing rules (cmd.exe) a. determine what cmd.exe will see as the quoted parts ┌─┐

&<>^|()@" "! b. escape the special characters not in double quoted parts:

( the escape character for cmd.exe is ^ ) ┌─┐

^&^<^>^^^|^(^)^@" "^!

↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ c. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: ^&^<^>^^^|^(^)^@" "^!

TestParams8_6.bat REM both lines should produce the same result

ShowParams.exe "&<>^|()@ !"

ShowParams.exe ^&^<^>^^^|^(^)^@" "^!



(Note: Since this is a batch file, if there were any % we would need to double them.)



Example 8.7: Command Line to C/C++ Program

Example 8.7: &<>^|@()!"&<>^|@() ! Parameter Start with the parameter you want &<>^|@()!"&<>^|@()! 1. Apply the Microsoft C/C++ parsing rules a. replace: literal " with \"

literal \" with \\\"

literal \\" with \\\\\" &<>^|@()!\"&<>^|@()!

↑ b. enclose the whole parameter in double quotes "&<>^|@()!\"&<>^|@()!"

↑ ↑ 2. Apply the Command Prompt parsing rules (cmd.exe) a. determine what cmd.exe will see as the quoted parts ┌──────────┐ ┌───

"&<>^|()@!\"&<>^|@()!" we have a problem in that the final " is interpreted by cmd.exe as opening a double quoted part. To avoid this, escape that last " ( the escape character for cmd.exe is ^ ) ┌──────────┐

"&<>^|()@!\"&<>^|@()!^"

↑ b. escape the other special characters not in double quoted parts:

( the escape character for cmd.exe is ^ ) ┌──────────┐

"&<>^|@()!\"^&^<^>^^^|@()!^"

↑ ↑ ↑ ↑ ↑ c. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: "&<>^|@()!\"^&^<^>^^^|@()!^" Another way to get the same result would be at step 1b don't enclose the parameter in double quotes, then escape all special characters including the double quote so it doesn't start a double quoted part. ^&^<^>^^^|@()!\^"^&^<^>^^^|@()!

↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑

TestParams8_7.bat REM both lines should produce the same result

ShowParams.exe "&<>^|@()!\"^&^<^>^^^|@()!^"

ShowParams.exe ^&^<^>^^^|@()!\^"^&^<^>^^^|@()!



(Note: Since this is a batch file, if there were any % we would need to double them.)



Example 8.8: Command Line to C/C++ Program

Example 8.8a: &<>^|@() !"&<>^|@() ! Parameter Start with the parameter you want (parameter includes leading and trailing double quotes, plus a double quote inside, and two spaces) "&<>^|@() !"&<>^|@() !" 1. Apply the Microsoft C/C++ parsing rules a. replace: literal " with \"

literal \" with \\\"

literal \\" with \\\\\" \"&<>^|@() !\"&<>^|@() !\"

↑ ↑ ↑ b. enclose the whole parameter in double quotes ┌──────────────────────────┐

"\"&<>^|@() !\"&<>^|@() !\""

↑ ↑ 2. Apply the Command Prompt parsing rules (cmd.exe) a. determine what cmd.exe will see as the quoted parts ┌─┐ ┌───────────┐┌───

"\"&<>^|@() !\"&<>^|@() !\"" we have a problem in that the final " is interpreted by cmd.exe as opening a double quoted part. To avoid this, escape that last " ( the escape character for cmd.exe is ^ ) ┌─┐ ┌───────────┐

"\"&<>^|@() !\"&<>^|@() !\"^"

↑ b. escape the special characters not in double quoted parts:

( the escape character for cmd.exe is ^ ) ┌─┐ ┌───────────┐

"\"^&^<^>^^^|@() !\"&<>^|@() !\"^"

↑ ↑ ↑ ↑ ↑ c. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: "\"^&^<^>^^^|@() !\"&<>^|@() !\"^" Another way to get the same result would be at step 2a just escape all special characters including all double quotes so there are no double quoted parts. ^"\^"^&^<^>^^^|@() !\^"^&^<^>^^^|@() !\^"^"

↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ Example 8.8b: &<>^|@() !"&<>^|@() ! Parameter Start with the parameter you want (same as 3a) "&<>^|@() !"&<>^|@() !" 1. Apply the Microsoft C/C++ parsing rules a. replace: literal " with \"

literal \" with \\\"

literal \\" with \\\\\" \"&<>^|@() !\"&<>^|@() !\"

↑ ↑ ↑ b. enclose spaces in double quotes

(A double quoted part can be anywhere within a parameter) ┌─┐ ┌─┐

\"&<>^|@()" "!\"&<>^|@()" "!\"

↑ ↑ ↑ ↑ 2. Apply the Command Prompt parsing rules (cmd.exe) a. determine what cmd.exe will see as the quoted parts ┌────────┐ ┌──┐ ┌─┐ ┌───

\"&<>^|@()" "!\"&<>^|@()" "!\" once again we have a problem in that the final " is interpreted by cmd.exe as opening a double quoted part. To avoid this, escape that last " ( the escape character for cmd.exe is ^ ) ┌────────┐ ┌──┐ ┌─┐

\"&<>^|@()" "!\"&<>^|@()" "!\^"

↑ b. escape the other special characters not in double quoted parts:

( the escape character for cmd.exe is ^ ) ┌────────┐ ┌──┐ ┌─┐

\"&<>^|@()" "!\"^&^<^>^^^|@()" "!\^"

↑ ↑ ↑ ↑ ↑ c. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: \"&<>^|@()" "!\"^&^<^>^^^|@()" "!\^" Another way to get the same result would be at step 2a just escape all special characters including all double quotes so there are no double quoted parts. \^"^&^<^>^^^|@()^" ^"!\^"^&^<^>^^^|@()^" ^"!\^"

↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑

TestParams8_8.bat REM all 4 lines should produce the same result

ShowParams.exe "\"^&^<^>^^^|@() !\"&<>^|@() !\"^"

ShowParams.exe ^"\^"^&^<^>^^^|@() !\^"^&^<^>^^^|@() !\^"^"

ShowParams.exe \"&<>^|@()" "!\"^&^<^>^^^|@()" "!\^"

ShowParams.exe \^"^&^<^>^^^|@()^" ^"!\^"^&^<^>^^^|@()^" ^"!\^"



(Note: Since this is a batch file, if there were any % we would need to double them.)



Example 8.9: Command Line to C/C++ Program

Example 8.9a: "C:\TEST A\" Parameter Start with the parameter you want (parameter includes double quotes and a space) "C:\TEST A\" 1. Apply the Microsoft C/C++ parsing rules a. replace: literal " with \"

literal \" with \\\"

literal \\" with \\\\\" \"C:\TEST A\\\"

↑ ↑↑ b. enclose the whole parameter in double quotes

(because there's a space in the parameter) ┌───────────────┐

"\"C:\TEST A\\\""

↑ ↑ 2. Apply the Command Prompt parsing rules (cmd.exe) a. determine what cmd.exe will see as the quoted parts ┌─┐ ┌┐

"\"C:\TEST A\\\"" b. escape the special characters not in double quoted parts:

( the escape character for cmd.exe is ^ ) No special characters to escape

(as seen by cmd.exe) c. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: "\"C:\TEST A\\\"" Another way to get the same result would be at step 2a just escape all special characters including all double quotes so there are no double quoted parts. ^"\^"C:\TEST A\\\^"^"

↑ ↑ ↑ ↑ Example 8.9b: "C:\TEST A\" Parameter Start with the parameter you want (same as 4a) "C:\TEST A\" 1. Apply the Microsoft C/C++ parsing rules a. replace: literal " with \"

literal \" with \\\"

literal \\" with \\\\\" \"C:\TEST A\\\"

↑ ↑↑ b. enclose spaces in double quotes

(A double quoted part can be anywhere within a parameter) \"C:\TEST" "A\\\"

↑ ↑ 2. Apply the Command Prompt parsing rules (cmd.exe) a. determine what cmd.exe will see as the quoted parts ┌───────┐ ┌────┐ \"C:\TEST" "A\\\" b. escape the special characters not in double quoted parts:

( the escape character for cmd.exe is ^ ) It's all in double quoted parts.

(as seen by cmd.exe) c. if this will be placed in a batch file, double the % characters no % in parameter Result: To get desired parameter use this: \"C:\TEST" "A\\\" Another way to get the same result would be at step 2a just escape all special characters including all double quotes so there are no double quoted parts. \^"C:\TEST^" ^"A\\\^"

↑ ↑ ↑ ↑

TestParams8_9.bat REM all 4 lines should produce the same result

ShowParams.exe "\"C:\TEST A\\\""

ShowParams.exe ^"\^"C:\TEST A\\\^"^"

ShowParams.exe \"C:\TEST" "A\\\"

ShowParams.exe \^"C:\TEST^" ^"A\\\^"



(Note: Since this is a batch file, if there were any % we would need to double them.)



Example 8.10: Command Line to C/C++ Program

Example 8.10a: "C:\TEST %&^ A\" Parameter Start with the parameter you want "C:\TEST %&^ A\" 1. Apply the Microsoft C/C++ parsing rules a. replace: literal " with \"

literal \" with \\\"

literal \\" with \\\\\" \"C:\TEST %&^ A\\\"

↑ ↑↑ b. enclose the whole parameter in double quotes ┌───────────────────┐

"\"C:\TEST %&^ A\\\"" 2. Apply the Command Prompt parsing rules (cmd.exe) a. determine what cmd.exe will see as the quoted parts ┌─┐ ┌┐

"\"C:\TEST %&^ A\\\"" b. escape the special characters not in double quoted parts:

( the escape character for cmd.exe is ^ ) ┌─┐ ┌┐

"\"C:\TEST ^%^&^^ A\\\""

↑ ↑ ↑ c. if this will be placed in a batch file, double the % characters ┌─┐ ┌┐

"\"C:\TEST ^%%^&^^ A\\\""

↑ Result: To get desired parameter use this: "\"C:\TEST ^%%^&^^ A\\\"" Another way to get the same result would be at step 2a just escape all special characters including all double quotes so there are no double quoted parts. ^"\^"C:\TEST %^&^^ A\\\^"^"

↑ ↑ ↑ ↑ ↑ ↑ c. and if this will be placed in a batch file, double the % characters ^"\^"C:\TEST %%^&^^ A\\\^"^"

↑ Example 8.10b: "C:\TEST %&^ A\" Parameter Start with the parameter you want (same as 5a) "C:\TEST %&^ A\" 1. Apply the Microsoft C/C++ parsing rules a. replace: literal " with \"

literal \" with \\\"

literal \\" with \\\\\" \"C:\TEST %&^ A\\\"

↑ ↑↑ b. enclose spaces in double quotes

(A double quoted part can be anywhere within a parameter) ┌─┐ ┌─┐

\"C:\TEST" "%&^" "A\\\"

↑ ↑ ↑ ↑ 2. Apply the Command Prompt parsing rules (cmd.exe) a. determine what cmd.exe will see as the quoted parts ┌───────┐ ┌───┐ ┌────┐

\"C:\TEST" "%&^" "A\\\" b. escape the special characters not in double quoted parts:

( the escape character for cmd.exe is ^ ) it's all in double quoted parts

(as seen by cmd.exe) c. if this will be placed in a batch file, double the % characters \"C:\TEST" "%%&^" "A\\\"

↑ Result: To get desired parameter use this: "\"C:\TEST ^%%^&^^ A\\\"" Another way to get the same result would be at step 2a just escape all special characters including all double quotes so there are no double quoted parts. \^"C:\TEST^" ^"%^&^^^" ^"A\\\^"

↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ c. and if this will be placed in a batch file, double the % characters \^"C:\TEST^" ^"%%^&^^^" ^"A\\\^"

↑

TestParams8_10.bat REM all 4 lines should produce the same result

ShowParams.exe "\"C:\TEST ^%%^&^^ A\\\""

ShowParams.exe ^"\^"C:\TEST %%^&^^ A\\\^"^"

ShowParams.exe "\"C:\TEST ^%%^&^^ A\\\""

ShowParams.exe \^"C:\TEST^" ^"%%^&^^^" ^"A\\\^"



9 How To Pass A Parameter to:

a Batch File from the Command Line

To get a parameter into a batch file you need to work backwards through the two parsings it will go through:

The cmd.exe command line parser parses your command line & parameters and then starts running the batch file The batch file retrieves the parameters, but when you use them, they get parsed again. Here's an example: a batch file line contains %1 %1 is replaced with your parameter that line is then sent to the command line parser for execution, where it gets parsed

Start with the parameter you want Apply the command line parsing rules that cmd.exe will apply when you enter the command and parameters [ sec. 7 ] Again, apply the command line parsing rules that cmd.exe will apply when you enter the command and parameters [ sec. 7 ]

Apply the Command Line Parser (cmd.exe) parsing rules determine what cmd.exe will see as the quoted parts escape the special characters < > | & ^ not in a double quoted part again, escape the special characters < > | & ^ not in a double quoted part •the escape character for cmd.exe is ^

•it may also be necessary to escape ( ) @ !

•it's OK to escape everything If your command line will be placed in a batch file, double the % character

The Command Line to Batch File Rules:

Use ^^^ outside double quotes to escape < > & | ^

to insert < use ^^^<

to insert > use ^^^>

to insert | use ^^^|

to insert ^ use ^^^^

to insert < use ^^^< to insert > use ^^^> to insert | use ^^^| to insert ^ use ^^^^ Sometimes it may be necessary to also escape ( ) @ !

It's OK to escape everything

Note ^ is also the line continuation character. Combining the two steps above gives us the following rules:

Command line arguments can be separated by an unquoted semicolon or comma. This affects builtin commands like COPY and batch file parameters. If the command is called by name (no path), the following slash is treated a separator, as in DIR/P. This affects both builtin commands and external commands. Note: If you want to use NT-style slash-separated paths with DOS utilities you have to quote them (as in TYPE "C:/BOOT.INI").

Example 9.1: Command Line to Batch File Example 9.1: &<>^|()@! Parameter Start with the parameter you want &<>^|()@! 1. Apply the Command Line to Batch File parsing rules a. determine what cmd.exe will see as the quoted parts: it's all unquoted b. escape the special characters not in double quoted parts: ^^^&^^^<^^^>^^^^^^^|()@!

↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑ Result: To get desired parameter use this: ^^^&^^^<^^^>^^^^^^^|()@! Sample Test File: TestParams9_1.bat ShowParams.bat ^^^&^^^<^^^>^^^^^^^|()@!



(Note: Since this is a batch file, if there were any % we would need to double them. WARNING! When executing a batch file from inside another batch file, the original batch file is terminated before the other one starts. This method of invoking a batch file from another is usually referred to as chaining.)



Example 9.2: Command Line to Batch File Example 9.2: &<>^|()@! Parameter Start with the parameter you want ┌─────────┐

"&<>^|()@!" 1. Apply the Command Line to Batch File parsing rules a. determine what cmd.exe will see as the quoted parts: it's all in a double quoted part b. escape the special characters not in double quoted parts: Nothing to escape because it's all in a double quoted part

(as seen by cmd.exe) Result: To get desired parameter use this: "&<>^|()@!" Sample Test File: TestParams9_2.bat ShowParams.bat "&<>^|()@!"



(Note: Since this is a batch file, if there were any % we would need to double them. WARNING! When executing a batch file from inside another batch file, the original batch file is terminated before the other one starts. This method of invoking a batch file from another is usually referred to as chaining.)



Example 9.3: Command Line to Batch File Example 9.3: &<>^|()@!"&<>^|()@! Parameter Start with the parameter you want &<>^|()@!"&<>^|()@! 1. Apply the Command Line to Batch File parsing rules a. determine what cmd.exe will see as the quoted parts: ┌─────────┐ ┌───

"&<>^|()@!"&<>^|()@!" we have a problem in that the final " is interpreted by cmd.exe as opening a double quoted part. To avoid this, escape that last " ┌─────────┐

"&<>^|()@!"&<>^|()@!^^^"

↑↑↑ b. escape the special characters not in double quoted parts: ┌─────────┐

"&<>^|()@!"^^^&^^^<^^^>^^^^^^^|()@!^^^"

↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑ Result: To get desired parameter use this: "&<>^|()@!"^^^&^^^<^^^>^^^^^^^|()@!^^^" Though not necessary, it's OK to escape the rest of the characters: ┌─────────┐

"&<>^|()@!"^^^&^^^<^^^>^^^^^^^|^^^(^^^)^^^@^^^!^^^"

↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑ An easier way to get the same result would be to escape the " within the parameter so it doesn't end the double quoted part. Then nothing else needs to be escaped since it's all in a double quoted part. ┌────────────────────┐

"&<>^|()@!\"&<>^|()@!"

↑ Another way to get the same result would be to escape all the special characters including all the " so nothing is in a double quoted part: ^^^"^^^&^^^<^^^>^^^^^^^|()@!^^^"^^^&^^^<^^^>^^^^^^^|()@!^^^" As before, though it's not necessary, it is OK to escape the rest of the characters: ^^^"^^^&^^^<^^^>^^^^^^^|^^^(^^^)^^^@^^^!^^^"^^^&^^^<^^^>^^^^^^^|^^^(^^^)^^^@^^^!^^^"

↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑ ↑↑↑ Sample Test Files:

all 5 test files should produce the same result TestParams9_3a.bat ShowParams.bat "&<>^|()@!"^^^&^^^<^^^>^^^^^^^|()@!^^^"



(Note: Since this is a batch file, if there were any % we would need to double them. WARNING! When executing a batch file from inside another batch file, the original batch file is terminated before the other one starts. This method of invoking a batch file from another is usually referred to as chaining.)

TestParams9_3b.bat ShowParams.bat "&<>^|()@!"^^^&^^^<^^^>^^^^^^^|^^^(^^^)^^^@^^^!^^^"



(Note: Since this is a batch file, if there were any % we would need to double them. WARNING! When executing a batch file from inside another batch file, the original batch file is terminated before the other one starts. This method of invoking a batch file from another is usually referred to as chaining.)

TestParams9_3c.bat ShowParams.bat "&<>^|()@!\"&<>^|()@!"



(Note: Since this is a batch file, if there were any % we would need to double them. WARNING! When executing a batch file from inside another batch file, the original batch file is terminated before the other one starts. This method of invoking a batch file from another is usually referred to as chaining.)

TestParams9_3d.bat ShowParams.bat ^^^"^^^&^^^<^^^>^^^^^^^|()@!^^^"^^^&^^^<^^^>^^^^^^^|()@!^^^"



(Note: Since this is a batch file, if there were any % we would need to double them. WARNING! When executing a batch file from inside another batch file, the original batch file is terminated before the other one starts. This method of invoking a batch file from another is usually referred to as chaining.)

TestParams9_3e.bat ShowParams.bat ^^^"^^^&^^^<^^^>^^^^^^^|^^^(^^^)^^^@^^^!^^^"^^^&^^^<^^^>^^^^^^^|^^^(^^^)^^^@^^^!^^^"



(Note: Since this is a batch file, if there were any % we would need to double them. WARNING! When executing a batch file from inside another batch file, the original batch file is terminated before the other one starts. This method of invoking a batch file from another is usually referred to as chaining.)

all 5 test files should produce the same result

10. How To Pass A Parameter to:

a VBScript, JScript, or WSH Script

from the Command Line

®

®

Example: > C:\WINDOWS\system32\wscript.exe ShowParams.vbs hello goodbye Friday

“If you want to get picky, the truth is that you can’t read command-line arguments using VBScript; that’s because VBScript doesn’t know anything about command-line arguments. But that’s all right; after all, VBScript doesn’t have to know anything about command-line arguments. That’s because Windows Script Host takes care of all that stuff. “Any time you supply a command-line argument to a script that runs under Windows Script Host (that includes JScript scripts as well as VBScript scripts) those arguments are automatically stored in the Wscript.Arguments collection.”



— Hey, Scripting Guy! 2008 Winter Scripting Games: Retrieving Command-Line Arguments

http://www.microsoft.com/technet/scriptcenter/funzone/games/tips08/gtip0104.mspx

GetCommandLine()

wscript!SplitCommandLine()

10.1 The WSH Command Line Parameter Parsing Rules:

parameters are separated by a space or tab (multiple spaces/tabs OK)

" begins and ends the double quoted part)

all characters within a double quoted part are accepted, including spaces and tabs)

The " character itself is always stripped from the parameter)

(Note this means you can not pass a double quote as part of a parameter.) )

10.2 The Microsoft ® Windows ® Script Host (WSH) Command Line Parameter Parsing Algorithm:

The steps to create your parameter are:Note that you can't have a space within a parameter without having some double quotes around it somehow. All spaces must be within a double quoted part. Otherwise the space will act as a parameter delimiter. (Same with tabs, semicolons, commas.)VBScript (.vbs), JScript (.js), and WSH (.wsh) scripts are run by program wscript.exe, which is the MicrosoftWindowsScript Host (WSH). WSH is a language-independent scripting host for Windows Script compatible scripting engines. Microsoft provides both Microsoft Visual Basic Script and JScript scripting engines with WSH. Windows Script Host executes scripts that exist outside an HTML or ASP page and that stand on their own as text files.When you run ShowParams.vbs you'll notice the window title says, "Window Script Host".On startup wscript.exe callsto get the command line, then calls, which parses off the parameters. The rules for WSH command line parameter parsing are simple:The following algorithm was reverse engineered by disassembling and examining wscript.exe and cscript.exe:

Algorithm:

call Kernel32.dll!GetCommandLine() to get the command line make a copy of the command line call wscript!SplitCommandLine() dry run to determine the number of parameters allocate space for argv[] array call wscript!SplitCommandLine() to parse parameters and fill in argv[] array

wscript!SplitCommandLine()

argv[]

Splitting the Command Line to get Parameters start with: ShowParams.exe hello goodbye Friday

end with: ShowParams.exe0hello0goodbye0Friday0

↑ ↑ ↑ ↑

argv[] │ │ │ │

param 0: ───┘ │ │ │

param 1: ──────────────────┘ │ │

param 2: ────────────────────────┘ │

param 3: ────────────────────────────────┘

loop parse off next parameter: skip over spaces, tabs clear " flag save starting address of this parameter LOOP process this character: If space or tab if " flag set accept this space or tab as part of the parameter else write a 0 here to terminate this parameter and goto parse off next parameter Else if " toggle " flag, strip " (shift rest of line left 1 char) move to next char ENDLOOP endloop

Notes: " flag - set if we're currently inside a double quoted part

The algorithm only looks for {space}, {tab}, and ("). All other characters are just characters.

You can't pass a double quote (") as part of a parameter because double quotes are always stripped off.

10.3 Putting it together:

First the cmd.exe command line parser parses your command & parameters, using its cmd.exe parsing rules. It builds a command line, and calls CreateProcess() passing it the command line it built. Then the Windows Script Host retrieves that resulting command line and parses off the parameters, using its WSH parsing rules.

Start with the parameter you want Apply the WSH parsing rules that Windows Script Host will apply when parsing the command line it retrieves Then apply the command line parsing rules that cmd.exe will apply when you enter the command and parameters

Apply the WSH parsing rules: if there are any spaces or tabs within a parameter, add some " so they are enclosed in a double quoted part ┌───────────────┐

•you may enclose the entire parameter in double quotes: "Call Me Ishmael"

•you may enclose the entire parameter in double quotes: ┌─┐ ┌─┐

•or you may enclose just the spaces in double quotes: Call" "Me" "Ishmael

•or you may enclose just the spaces in double quotes: ┌────────┐

•or you can make a mess of it: Ca"ll Me Is"hmael

Apply the Command Line Parser (cmd.exe) parsing rules determine what cmd.exe will see as the double quoted parts escape the special characters < > | & ^ not in a double quoted part •the escape character for cmd.exe is ^

•it may also be necessary to escape ( ) @ !

•it's OK to escape everything If your command line will be placed in a batch file, double the % character11

10.4 Sample Scripts:

ShowParams.vbs If Wscript.Arguments.Count = 0 Then

Wscript.echo "No parameters found"

Else

i=0

Do until i = Wscript.Arguments.Count

Parms = Parms & "Param " & i & " = " & Wscript.Arguments(i) & " " & vbcr

i = i+1

loop

Wscript.echo parms

End If



ShowParams.js if (WScript.Arguments.Count() ==0) {

WScript.Echo("No parameters found");

}

else {

var objArgs = WScript.Arguments;

var parms = ""

for (i=0, n=objArgs.length; i<n; i++) {

parms += '

Param '+i+'='+objArgs(i);

}

WScript.Echo(parms);

}



11. How To Pass A Parameter to

a Perl script from the command line

MSVCRT.dll!__getmainargs

Start with the parameter you want Apply the Microsoft C/C++ parsing rules that ShowParams.exe will apply when parsing the command line it retrieves [ sec. 5 ] Apply the command line parsing rules that cmd.exe will apply when you enter the command and parameters [ sec. 7 ]

ShowParams.pl if ( $#ARGV < 0 ) {

print "No parameters

";

}

else {

my $i = 0;

my $Parms = "";

while ( $i <= $#ARGV ) {

$Parms .= "Param $i = $ARGV[$i]

";

$i ++;

}

print $Parms;

}



12. How To Pass A Parameter to

a Python script from the command line

Start with the parameter you want Apply the Microsoft C/C++ parsing rules that ShowParams.exe will apply when parsing the command line it retrieves [ sec. 5 ] Apply the command line parsing rules that cmd.exe will apply when you enter the command and parameters [ sec. 7 ]

ShowParams2.py #!/usr/bin/python

# Filename: using_sys.py

import sys

print 'The command line arguments are:'

for i in sys.argv:

print i

print '



The PYTHONPATH is', sys.path, '

'



ShowParams3.py #!/usr/bin/python

# Filename: using_sys.py

import sys

print( 'The command line arguments are:' )

for i in sys.argv:

print( i )

print( '



The PYTHONPATH is', sys.path, '

' )



13. How To Pass A Parameter to

a REXX script from the command line

8/24/2009 Under Construction...

ShowParams.rex NumParams = %0%

Param1 = %1%

Param2 = %2%

Param3 = %3%

MsgBox GetCommandLine=%string%`nNumParams = %0%`nParam1 = %1%`nParam2 = %2%`nParam3 = %3%

ExitApp



14. How To Pass A Parameter to

a RUBY script from the command line

(file Win32.c routine rb_w32_cmdvector, the memcpy statement needs to copy one more character to include the trailing NULL.)

ShowParams.rb ARGV.each do|a|

puts "Argument: #{a}"

end



15. How To Pass A Parameter to

an AutoHotkey script from the command line

Start with the parameter you want Apply the Microsoft C/C++ parsing rules that ShowParams.exe will apply when parsing the command line it retrieves [ sec. 5 ] Apply the command line parsing rules that cmd.exe will apply when you enter the command and parameters [ sec. 7 ]

ShowParams.ahk

NumParams = %0%

Param1 = %1%

Param2 = %2%

Param3 = %3%

result := DllCall("kernel32\GetCommandLineW")

pointer := result

string := DllCall("MulDiv","int",pointer,"int",1,"int",1,"str")

MsgBox GetCommandLine=%string%`nNumParams = %0%`nParam1 = %1%`nParam2 = %2%`nParam3 = %3%

ExitApp



http://daviddeley.com

Last update: July 5, 2020

Thank you to Stefan Kanthak for pointing out some spelling errors.

builds anarray of where each parameter begins, and writes a terminating NULL at the end of each parameter.When you launch a VBScript, JScript, or WSH script from the command line, your command line goes through two parsers:So to get a parameter into a VBScript, JScript, or WSH script, you need to work backwards through the two parsers it will go through:The steps to create your parameter are:Sample 'Show Parameters' scripts:VBScript:JScript:ActivePerl and Strawberry Perl both callwhich follows the Microsoft C/C++ Parameter Parsing Rules. Therefore the steps are:See sec. 8: How To Pass A Parameter to: a C/C++ Program from the Command Line above.Sample 'Show Parameters' Perl script:Python™ is written in C and obtains its parameters via the argv[] vector. It thus uses the Microsoft C/C++ Parameter Parsing Rules. Therefore the steps are:See sec. 8: How To Pass A Parameter to: a C/C++ Program from the Command Line above.Sample 'Show Parameters' Python script for Python version 2:Sample 'Show Parameters' Python script for Python version 3:Sample 'Show Parameters' REXX script:Ruby attempts to mimic the Microsoft C/C++ parameter parsing rules, but the current versions, 1.8.6 & 1.9.1, have a bug in them so they don't work properly.Sample 'Show Parameters' RUBY script:AutoHotkey is written in C++ and obtains its parameters via the __argv[] vector. Thus it uses the Microsoft C/C++ Parameter Parsing Rules. Therefore the steps are:See sec. 8: How To Pass A Parameter to: a C/C++ Program from the Command Line above.Sample 'Show Parameters' AutoHotkey script:David Deley © 2009