7

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 ?

5 Answers 5

15

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
0
2

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]!
1
  • Thanks for the precise clarification about it.
    – Yair Nevet
    Mar 3, 2015 at 11:01
0

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.

1
  • Thanks, but I'm passing several parameters. Good to know anyway!
    – Yair Nevet
    Mar 2, 2015 at 13:12
0

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
2
  • 1
    When posting code, consider using proper formatting: code should be indented by 4 spaces, in-line code should be enclosed in "`".
    – user1143634
    Dec 7, 2017 at 10:27
  • @Ivan Thanks for correcting the formatting. I am new here, just started helping others Dec 7, 2017 at 15:31
0

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[].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.