Reputation: 13003
My batch script is used to process a list of strings and I would like to parameterize it so it will accept this list as a parameter from the user.
This is how my code is processing this list currently:
set QUEUES=cars plans others
FOR %%q IN (%QUEUES%) DO Call :defineQueues %%q
How should I pass this list to the QUEUES
varibale as a parameter?
For example, how should I pass it to this script:
myScript.bat ?
Upvotes: 8
Views: 22238
Reputation: 404
If you want to let your script be used in this way:
myScript.bat "cars plans others"
You can try to program in this way:
setlocal EnableDelayedExpansion
set string_of_strings = (%~1) // %1 is: "cars plans others", %~1" is: cars plans others", (%~1) is: (cars plans others)
for %%i in %string_of_strings% do echo %%i%NL%
The key is, %1 is of string but string_of_strings is kind of string[].
Upvotes: 0
Reputation: 113
You can call it as myScript.bat %QUEUES%
and you can use below code to fetch it in your myScript.bat
:
setlocal EnableDelayedExpansion
FOR %%q in (!%1!) DO echo %%q
Upvotes: 0
Reputation: 67216
Excuse me. I read this question and I can't resist the temptation of clarify a couple points about it.
A Batch file usually receive several words as parameters, like these ones:
myScript.bat cars plans others
Previous Batch file receive 3 parameters that may be processed via %1, %2 and %3 in the usual way. If you want that a single parameter receive several words, all of them must be enclosed in quotes:
myScript.bat "cars plans others"
Previous Batch file receive one parameter that contain several words that may be processed via %1. Note that the following lines are exactly the same than previous examples:
set QUEUES=cars plans others
myScript.bat %QUEUES%
Previous Batch file receive 3 parameters, and:
myScript.bat "%QUEUES%"
... previous Batch file receive one parameter.
A list is a variable that contain several values separated by spaces, like this one:
set QUEUES=cars plans others
You may pass this list to the Batch file as parameter this way:
myScript.bat QUEUES
Previous Batch file receive one parameter that is a list variable. To process the values of the list in myScript.bat, use this method:
setlocal EnableDelayedExpansion
FOR %%q in (!%1!) DO echo %%q
An array is a variable comprised of several elements identified by a numeric subscript, like this one:
set NAMES[1]=cars
set NAMES[2]=plans
set NAMES[3]=others
An array usually have a simple way to know the number of elements in it; for example:
set NAMES.length=3
You may pass this array of strings to the Batch file as parameter this way:
myScript.bat NAMES
Previous Batch file receive one parameter that is an array. To process the elements of the array in myScript.bat, use this method:
setlocal EnableDelayedExpansion
FOR /L %%i in (1,1,!%1.length!) DO echo !%1[%%i]!
Upvotes: 2
Reputation: 1471
You have to enclose your string with quotes:
myScript.bat "cars plans others"
Then %1
will be equals to "cars plans others"
Or %~1%
to remove the quotes and only get cars plans others
Otherwise, you will get 3 different parameter values:
myScript.bat cars plans others
%1 => cars
%2 => plans
%3 => others
Upvotes: 17
Reputation: 1982
Alternatively, if you only pass your QUEUES values on the command line, you can use the %* batch file operator, which is a wildcard reference to all the arguments on the line.
@echo off
FOR %%q IN (%*) DO echo %%q
Executing the batch file as follows:
x.bat cars plans others
gives the output:
C:\junk>x.bat cars plans others
cars
plans
others
C:\junk>
If you pass any other arguments on the command together with the QUEUES elements, it is not that simple to 'shift' the other arguments out.
Upvotes: 1