The batch file args.bat intended for testing:
@echo off echo Arguments 1 to 4: echo.%~1 echo.%~2 echo.%~3 echo.%~4
The batch file uses ~ to remove the leading quotation mark and trailing, and should display the following values ββto the user:
Argument 1 This is "Argument 2".
For argument 1 it is easy:
args.bat "Argument 1"
However, for argument 2, everything does not work well, no matter what escape sequence I try (I found several here in 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"."
Dropping with double quotes somehow passes double quotes to:
>args.bat "Argument 1" "This is ""Argument 2""." Arguments 1 to 4: Argument 1 This is ""Argument 2"".
Hiding with a stroke:
>args.bat "Argument 1" "This is ^"Argument 2^"." Arguments 1 to 4: Argument 1 This is "Argument 2"."
Escape with a backslash:
>args.bat "Argument 1" "This is \"Argument 2\"." Arguments 1 to 4: Argument 1 This is \"Argument 2\"."
Make sure I try a whole bunch of other things (ultimately, thinking about rudely forcing a solution, but asking a question first), each of which leads to a funny release, 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 the parameter to my batch file as I want without changing the batch file? If not, is there a way to run cmd in special mode so that it handles parameters like I want?
I have seen solutions that search for and replace double quotes, but I really want the batch file not to change.