6

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 ?
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108

5 Answers5

13

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
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
Rubik
  • 1,431
  • 1
  • 18
  • 24
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]!
Aacini
  • 65,180
  • 12
  • 72
  • 108
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.

Community
  • 1
  • 1
Moshe Rubin
  • 1,944
  • 1
  • 17
  • 37
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
TT.
  • 15,774
  • 6
  • 47
  • 88
Vinita Gupta
  • 103
  • 1
  • 1
  • 10
  • 1
    When posting code, consider using proper formatting: code should be indented by 4 spaces, in-line code should be enclosed in "`". –  Dec 07 '17 at 10:27
  • @Ivan Thanks for correcting the formatting. I am new here, just started helping others – Vinita Gupta Dec 07 '17 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[].

Cary
  • 372
  • 1
  • 5
  • 14