7

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.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • possible duplicate of [Escaping Double Quotes in Batch Script](http://stackoverflow.com/questions/562038/escaping-double-quotes-in-batch-script) – Benjy Kessler May 10 '15 at 22:49
  • 1
    @BenjyKessler: I read the answers there and in my question you'll find I tried both, the double quote and circumflex escape characters. None of them work. – Thomas Weller May 10 '15 at 22:53

1 Answers1

7

You can't bring such an argument into %1 (or any other %<n>).

But with a little trick it's possible.

args.bat arg1 "This is !q!Argument 2!q!"

And args.bat looks like

setlocal EnableDelayedExpansion
set "q=""
echo * = %*
echo 1 = %~1
echo 2 = %~2

Or also this is possible

@echo off
setlocal EnableDelayedExpansion
set "arg1=%~1"
set "arg2=%~2"
echo * = %*
echo 1 = %arg1:""="%
echo 2 = %arg2:""="%

Then you can call it with args.bat "This is ""Argument 1"""

When you don't want to work with this technic you could also decide to parse the arguments by yourself.
Then you need to take all argumtents from %* and go through each character.

Edit
If you can't modify the scripts at all, then you could use the first solution one modification.

At the command prompt.

C:\SomeDir> cmd /V:ON 
C:\SomeDir> set "q=""
C:\SomeDir> args.bat argument1 "This is ^!q^!Argument 2^!q^!"

Or as one-liner

cmd /v:on /c set ^^^"q=^^^"^^^" ^& test "This is ^!q^!Argument 2^!q^!"

Using this enables the enabled delayed mode before entering the batch file, this can have some other undesired side effects in the called batch file if it doesn't handle this case properly.

jeb
  • 78,592
  • 17
  • 171
  • 225
  • 3
    But can it be done without editing the script at all? That's how I'm reading the question. I imagine this is for work and there's a script deployment system in place that requires all changes to be reviewed and tested thoroughly, which can cause delays days or weeks long. – SomethingDark May 11 '15 at 10:53
  • You are right, I added a solution which doesn't require script modifications – jeb May 11 '15 at 11:25