Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMD nested double quotes in argument

I am having problems with passing arguments to a batch function with nested double quotes.

Here is an example of a batch file:

@SET path_with_space="c:\test\filenamewith space.txt"
@CALL :FUNCTION blaat1, "blaat2 %path_with_space%"
@GOTO :EOF

:FUNCTION
@echo off
echo arg 1: %~1
echo arg 2: %~2
echo arg 3: %~3
GOTO :EOF

The output is:

arg 1: blaat1
arg 2: blaat2 "c:\test\filenamewith
arg 3: space.txt""

What should I do to make arg 2: blaat2 "c:\test\filenamewith space.txt"? Note that I cannot adjust the function or change the %path_with_space%. I can only control what is passed to the function.

like image 739
Davor Josipovic Avatar asked Sep 23 '12 11:09

Davor Josipovic


People also ask

How do I escape double quotes in CMD?

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.

What does %% mean in CMD?

%% is needed in a script to avoid ambiguities. "When working at the command line (not in a batch script) there is no possibility of any batch file parameters %1, %2 etc so the logic above is not followed and hence FOR parameters on the command line only need a single %." See details. – dosentmatter. Feb 27, 2021 at 6: ...

What does %% A mean in batch?

%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.

How do you pass quotes from the command line?

c\'v when you call your program. If you have single quotes in your program arguments and you don't want those arguments to be expanded, then surround them by double quotes, like this: "*. c'v". The program will get the string *.


1 Answers

Like dbenham said, it's seems impossible to escape a space in a parameter without quotes.
But it could be possible if you know how the receiver function gets the parameters.
Then you could tranfer the parameter via an escaped delayed variable, the variable will be expanded not in the call, it will be expanded just in the function.
And it's necessary that the parameters will be assigned in the function to variables, but this could be the case in a good and readable code.

setlocal EnableDelayedExpansion 
set path_with_space="c:\test\filenamewith space.txt"
@CALL :FUNCTION blaat1, "blaat2 ^!path_with_space^!"
GOTO :EOF

:FUNCTION
@echo off
echo arg 1: %~1
echo arg 2: %~2
echo arg 3: %~3
GOTO :EOF 

The output is:

arg 1: blaat1
arg 2: blaat2 "c:\test\filenamewith space.txt"
arg 3:

EDIT: Soltution with batch injection

This works even when delayed expansion should be always disabled.
But then you need to now how the parameters are expanded in the function.

@echo off
set path_with_space="c:\test\filenamewith space.txt"
CALL :FUNCTION 1 2 ""^^"&call:inject:""
exit/b

:inject
set arg1=blaat1
set arg2=blaat2 %path_with_space%
set arg3=none
exit /b

:FUNCTION
@echo off
set "arg1=%~1"
set "arg2=%~2"
set "arg3=%~3"
echo arg 1: %arg1%
echo arg 2: %arg2%
echo arg 3: %arg3%
GOTO :EOF
like image 149
jeb Avatar answered Sep 24 '22 03:09

jeb