0

I'm trying to parse a variable. Say NAMELIST = "AAA BBB CCC" and store each one as a variable. Then, these new variables must be used in another command. Eg:

perl.exe C:\action.pl <VAR1>
perl.exe C:\action.pl <VAR2>
perl.exe C:\action.pl <VAR3>

I'm new to Windows Batch so any help would be appreciated.

I am aware of this thread but don't fully understand the solution

Windows batch files: How to set a variable with the result of a command?

2 Answers 2

1

When you refer to "store each one in a variable", the involved concept here is array. You may split the words of NAMELIST variable into 3 array elements this way:

setlocal EnableDelayedExpansion
set i=0
for %%a in (%namelist%) do (
   set /A i=i+1
   set VAR!i!=%%a
)

This way, you may use each array element directly:

perl.exe C:\action.pl %VAR1%
perl.exe C:\action.pl %VAR2%
perl.exe C:\action.pl %VAR3%

Or, in a simpler way using a loop:

for /L %%i in (1,1,3) do perl.exe C:\action.pl !VAR%%i!

EDIT: You may use this method with an unlimited number of values in the NAMELIST variable, just use the previous value of %i% instead the 3 (better yet, change it by "n"). I also suggest you to use the standard array notation this way: VAR[%%i]:

setlocal EnableDelayedExpansion
set namelist=AAA BBB CCC DDD EEE FFF
set n=0
for %%a in (%namelist%) do (
   set /A n+=1
   set VAR[!n!]=%%a
)
for /L %%i in (1,1,%n%) do perl.exe C:\action.pl !VAR[%%i]!
Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly! Is there a way to eliminate specifying 3 in the call loop "for /L %%i in (1,1,3)"
0

Variables in batch can be set by using for loop. I'll try to explain the example given in the link.

for /f "delims=" %%a in (command) do @set theValue=%%a

Here For /F is used to break up the output into tokens. "delims=" means no explicit separator is given so "space" is assumed. %%a is like index variable of loop however instead of traditional index variable in programming languages where index variable has a numeric value index variables in batch can store the tokens i.e. output of command. set command then sets the variable "theValue" to %%a which holds the output/token of the command. Thus if the statement is:

 for /f "delims=" %%a in (`echo Hi everyone!`) do @set theValue=%%a

theValue will then hold "Hi" as it is the first token seprated by space.

You can specify your own separator as per your requirement. Hope this helps!

4 Comments

Thank Gaurav. But how do I use a variable say NAMELIST instead of "echo Hi everyone". I tried to replace it with a variable but it isn't working for me.
for /f "delims=" %%a in (echo %NAMELIST%) do @set theValue=%%a You tried this?
Yes. nevermind, it was a fault with our environment. I rebooted and tried and it works. Thanks!
Aacini's solution seems better though :P Sorry. But I can't upvote anyway. Just joined SO and need rep before I can upvote

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.