Reputation: 3651
I have a command that I want to pass into a batch file that contains quotes. What i want to do is pass in the following parameter as a single parameter into the bat file:
set parameter=-c myfirstparameter -p "also my first parameter"
C:\mybat.bat parameter
Unfortunatly this ends up being:
%1 = -c
%2 = myfirstparameter
etc
What I want is
%1 = -c myfirstparameter -p "also my first parameter"
Upvotes: 0
Views: 405
Reputation: 130809
You cannot do what you want. Batch uses [space], [comma], [semicolon], [equal], [tab], and [0xFF] as token delimiters when parsing arguments. If you want a token delimiter literal within an argument value, then the delimiter must be quoted. It is impossible to escape a token delimiter. Therefore it is impossible to have a single parameter that has both quoted and unquoted token delimiter literals.
Presumably the top code in your question is wrong. In order to get the results you describe, you must have
set parameter=-c myfirstparameter -p "also my first parameter"
C:\mybat.bat %parameter%
The best way to handle such a situation is to pass your parameter by reference, which requires a modification to your mybat.bat.
Instead of retrieving the value via %1
, you must enable delayed expansion and use !%1!
@echo off
setlocal enableDelayedExpansion
set "argument1=!%1!"
With such an arrangement, you would indeed use the following to call the script:
set parameter=-c myfirstparameter -p "also my first parameter"
C:\mybat.bat parameter
Upvotes: 3
Reputation:
Here is a working example of using a temp file (as found here) to pass the parameter:
FIRST.BAT
echo -c myfirstparameter -p "also my first parameter">dummy.txt
second.bat
SECOND.BAT
set /p param=<dummy.txt
echo %param%
Here is the raw output:
C:\temp\batchtest>first
echo -c myfirstparameter -p "also my first parameter" 1>dummy.txt
second.bat
set /p param= 0<dummy.txt
echo -c myfirstparameter -p "also my first parameter"
-c myfirstparameter -p "also my first parameter"
C:\temp\batchtest>
Upvotes: 1