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

batch-file, windows

Solution

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?

Problem

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?

Original source