Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving "=" (equal) characters in batch file parameters

I wrote a batch file to launch an application (the application is not mine, I cannot modify it). The batch file itself accepts some parameters. The application accepts other parameters. The batch file consumes all of its options, using SHIFT, then launch the application with the correct environment and pass the remaining parameters to the application. Example of calling the batch file:

script.bat -opt-1 -opt-2 /opt-a /opt-b=value

In the example, "-opt-1" and "-opt-2" are consumed by script.bat. At the end, it must call the original application with the parameters "/opt-a" and "/opt-b=value". The "=" sign in the last parameter is expected by the application, I cannot change it. It works well when I call the application directly from the command line.

But when I call it from the script, the application receives 2 parameters for "/opt-b=value": both "/opt-b" and "value". If I use "%*" when I call the application, the "=" signs are preserved, but all parameters are passed (including the parameters skipped using SHIFT).

Is there any way to pass only the last parameters and preserve "=" signs?

like image 573
Florent Georges Avatar asked Dec 13 '13 17:12

Florent Georges


2 Answers

Call your script.bat this way:

script.bat -opt-1 -opt-2 /opt-a "/opt-b=value"

and inside script.bat, call the application this way:

rem Consume -opt-1
shift
rem Consume -opt-2
shift
rem Call the application
application %1 %~2

EDIT: Response to the comments

Ok. Lets review this problem with detail.

The script.bat file below:

@echo off
rem Consume -opt-1
shift
rem Consume -opt-2
shift
rem Call the application
ECHO application %1 %~2

(please, note the ECHO command) correctly "call" the application with the second parameter including an equal-sign when its fourth parameter is enclosed in quotes, as the next screen output show:

C:\> script.bat -opt-1 -opt-2 /opt-a "/opt-b=value"
application /opt-a /opt-b=value

However, if the application IS A BATCH FILE, then it suffers from the same problem of the original script.bat file. You said that "It works well when I call the application directly from the command line". Well, this is not true:

C:\> type application.bat
@echo off
rem ** application.bat **
echo 1: %1
echo 2: %2
echo 3: %3
echo 4: %4 

C:\> application /opt-a /opt-b=value
1: /opt-a
2: /opt-b
3: value
4:

The parameters of a Batch file may be separated by comma, semicolon or equal-sign, besides spaces and tabs. This way, if "The "=" sign in the last parameter is expected by the application", then there is no way that the application be a Batch file and have no sense to test this method with a Batch file instead of the application.

Did you tested this solution with the real application?

like image 187
Aacini Avatar answered Sep 28 '22 10:09

Aacini


FOR /f "tokens=1*" %%x IN ("%*") DO ECHO application %%y

where 1 is the number of parameters to skip.


Testing...main .bat (q20572424.bat)

@ECHO OFF
SETLOCAL
ECHO master[%*]
FOR /f "tokens=2*" %%x IN ("%*") DO CALL q20572424a.bat %%y
FOR /f "tokens=1*" %%x IN ("%*") DO CALL q20572424a.bat %%y
FOR /f "tokens=*" %%x IN ("%*") DO CALL q20572424a.bat %%x
GOTO :EOF

Subsidiary .bat (q20572424a.bat)

@ECHO OFF
SETLOCAL
ECHO slave=[%*]
FOR /f "tokens=2*" %%x IN ("%*") DO CALL q20572424b.bat %%y
FOR /f "tokens=1*" %%x IN ("%*") DO CALL q20572424b.bat %%y
FOR /f "tokens=*" %%x IN ("%*") DO CALL q20572424b.bat %%x
GOTO :EOF

Second subsidiary .bat (q20572424b.bat)

@ECHO OFF
SETLOCAL
ECHO subslave=[%*]
GOTO :EOF

Results:

From running q20572424 -opt-1 -opt-2 /opt-a /opt-b=value

master[-opt-1 -opt-2 /opt-a /opt-b=value]
slave=[/opt-a /opt-b=value]
subslave=[]
subslave=[/opt-b=value]
subslave=[/opt-a /opt-b=value]
slave=[-opt-2 /opt-a /opt-b=value]
subslave=[/opt-b=value]
subslave=[/opt-a /opt-b=value]
subslave=[-opt-2 /opt-a /opt-b=value]
slave=[-opt-1 -opt-2 /opt-a /opt-b=value]
subslave=[/opt-a /opt-b=value]
subslave=[-opt-2 /opt-a /opt-b=value]
subslave=[-opt-1 -opt-2 /opt-a /opt-b=value]

Which appears to be correct. In each case, the subsidiary batch receives the parameters verbatim; the number of leading parameters removed is 2,1,0 for each call.

W7HP - works for me!

like image 34
Magoo Avatar answered Sep 28 '22 10:09

Magoo