The batch file args.bat
is for testing purposes:
@echo off
echo Arguments 1 to 4:
echo.%~1
echo.%~2
echo.%~3
echo.%~4
The batch file is using ~
to remove the leading and trailing quotation mark, and it should display the following values to the user:
Argument 1
This is "Argument 2".
For argument 1, it's easy:
args.bat "Argument 1"
However, for argument 2, things are not working well, no matter what escape sequence I try (I found several here on Stack Overflow):
// Didn't expect this to work, but for completeness
>args.bat "Argument 1" "This is "Argument 2"."
Arguments 1 to 4:
Argument 1
This is "Argument
2"."
Escaping with double quotes somehow passes the double quotes on:
>args.bat "Argument 1" "This is ""Argument 2""."
Arguments 1 to 4:
Argument 1
This is ""Argument 2"".
Escaping with circumflex:
>args.bat "Argument 1" "This is ^"Argument 2^"."
Arguments 1 to 4:
Argument 1
This is "Argument
2"."
Escape with backslash:
>args.bat "Argument 1" "This is \"Argument 2\"."
Arguments 1 to 4:
Argument 1
This is \"Argument
2\"."
Be sure I tried a whole bunch of other stuff (ultimately thinking of brute-forcing a solution, but asking the question first), each resulting in funny output but never what I want:
>args.bat "Argument 1" ^"This is ^^"Argument^" 2^^"."
Arguments 1 to 4:
Argument 1
This is "Argument" 2".
Is it possible to pass a parameter to my batch file like I want without changing the batch file? If not, is there a way of running cmd
in a special mode so that it treats the parameters like I want?
I have seen solutions doing a search and replace of double quotes, but I really want the batch file unchanged.