Use the data from input string as a set of variables

Posted on

QUESTION :

I want to extract some data from input string (e.g. name1,name2,name3…), and then use all of them as a variables (e.g. var1=name1, var2=name2, varN=nameN). The number of ‘names’ can be different. I want to apply a set of commands for these variables in a FOR loop later in the script.

ANSWER :

You can use a “for each” loop like so:

set params=param1,param2,param3

for %%p in (%params%) do (
    :: do something with %%p
)

Aside from a comma ,, the other valid delimiters are:

  • a semicolon ;
  • an equals sign =
  • a space character
  • a tab character

Example :

Script:

@echo off

set params=param1,param2,param3

for %%p in (%params%) do (
    echo %%p
)

Output:

param1
param2
param3

Leave a Reply

Your email address will not be published.