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

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!


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?