Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch Argument with quotes and spaces

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.

like image 939
Thomas Weller Avatar asked May 10 '15 22:05

Thomas Weller


People also ask

How do you handle spaces in a batch file?

When you send arguments, those with poison or space characters need to be doublequoted. Inside your batch file, if you no longer need the surrounding doublequotes, you'd remove them by using %~5 instead of %5 . Additionally the recommended syntax for the set command is Set "VariableName=VariableValue" .

What does %% mean in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is %% A?

%%a are special variables created by the for command to represent the current loop item or a token of a current line. for is probably about the most complicated and powerful part of batch files. If you need loop, then in most cases for has you covered.

How do you escape double quotes in DOS?

Double quotes enable escaping through the use of the backslash (\). For example, if the special character semicolon is a value that is double-quoted, it should be represented as backslash semicolon (\;) so that the Integration Engine knows that the actual value (;) should be used.


1 Answers

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.

like image 103
jeb Avatar answered Nov 15 '22 09:11

jeb