Solved

DOS batch file how to access multiple variables inside of FOR loop

Posted on 2013-11-21
8
3,768 Views
Last Modified: 2013-11-22
I have two lists or sets of variables if you want that I want to access both dynamically from inside a for loop via  DOS batch file. I know how to access one list in the loop but not the other. I have tried to lists and also one list with an array type set of variables. What I have so far looks like the following:

@if "%BATCH_DEBUG%"=="" echo off

SetLocal EnableDelayedExpansion

SET LIST_ITEMS=(ITEM1 ITEM2 ITEm3 ITEM4)

SET OTHER_ITEMS_0="TEST1"
SET OTHER_ITEMS_1="TEST2"
SET OTHER_ITEMS_2="TEST3"
SET OTHER_ITEMS_3="TEST4"

SET /A COUNT=0

for %%x in %LIST_ITEMS% do ( 
ECHO.

REM Line below will only echo out only "TEST1"
ECHO !OTHER_ITEMS_%COUNT%! 

REM Line below will echo out the full ist 'LIST_ITEMS'
REM ECHO %%x

SET /A COUNT+=1

)

:End
POPD
ECHO. & ECHO Press any key to close this window...
PAUSE >NUL
ENDLOCAL
EXIT /B 1

Open in new window

The script above will echo out the list 'LIST_ITEMS' but will only echo out the first variable 'OTHER_ITEMS_0'. I want to access all the variables that start with 'OTHER_ITEMS_' following the access of the array 'LIST_ITEMS'. I've tried ever combination of %, !, ", ... options I can think of and also I don't mind if all the variables of OTHER_ITEMS_ go into a list but again I want to access both list/arrays/variables in sequential order as list 'LIST_ITEMS' is accessed.

Thank You
0
Comment
Question by:atomicgs12
  • 3
  • 3
  • 2
8 Comments
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39667781
Give this a try.

@if "%BATCH_DEBUG%"=="" echo off

SetLocal EnableDelayedExpansion

SET LIST_ITEMS=ITEM1 ITEM2 ITEM3 ITEM4
SET OTHER_ITEMS=TEST1 TEST2 TEST3 TEST4

for %%x in (%LIST_ITEMS%) do (
  echo.%%x
  for %%y in (%OTHER_ITEMS%) do (
    echo.  %%y
  )
)

:End
POPD
ECHO. & ECHO Press any key to close this window...
PAUSE >NUL
ENDLOCAL
EXIT /B 1

Open in new window

~bp
0
 

Author Comment

by:atomicgs12
ID: 39668163
No that just produces the OTHER_ITEMS list four times, it's  loop inside of a loop. I need one iteration of each item, the end result I need to look like:
ITEM1 TEST1
ITEM2 TEST2
ITEM3 TEST3
ITEM4 TEST4

Please look closely at what I was asking for.

Thanks
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 400 total points
ID: 39668305
This is one way that works, using call:

@if "%BATCH_DEBUG%"=="" echo off

SetLocal EnableDelayedExpansion

SET LIST_ITEMS=(ITEM1 ITEM2 ITEm3 ITEM4)

SET OTHER_ITEMS_0="TEST1"
SET OTHER_ITEMS_1="TEST2"
SET OTHER_ITEMS_2="TEST3"
SET OTHER_ITEMS_3="TEST4"

SET /A COUNT=0

for %%x in %LIST_ITEMS% do ( 

REM Line below will only echo out only "TEST1"
CALL ECHO %%x is %%OTHER_ITEMS_!COUNT!%%

SET /A COUNT+=1

)

:End
POPD
ECHO Press any key to close this window...
PAUSE >NUL
ENDLOCAL
EXIT /B 1

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39668323
I sometimes do it like this too you can use a subroutine to read the array or define it manually like you did before.  This way is reading element "x" from the array by looping through the number of elements.

Steve

@if "%BATCH_DEBUG%"=="" echo off
SetLocal EnableDelayedExpansion

SET LIST_ITEMS=ITEM1 ITEM2 ITEM3 ITEM4
SET OTHER_ITEMS=TEST1 TEST2 TEST3 TEST4

call :ReadArray LIST_ITEMS
call :ReadArray OTHER_ITEMS

echo There are %count% elements:
SET OTHER_ITEMS_
SET LIST_ITEMS_

echo Now looping:

for /l %%c in (0,1,%count%) do ( 
  call ECHO %%LIST_ITEMS_%%c%% %%OTHER_ITEMS_%%c%%
)


ECHO Press any key to close this window...
PAUSE >NUL
ENDLOCAL
EXIT /B 1

:ReadArray
set count=-1
for %%x in (!%1!) do (
  SET /A COUNT+=1
  SET %1_!count!=%%x
)

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 100 total points
ID: 39668732
It would be helpful if you could further describe the real world situation you are working, in addition to this simulated example. Like where are the list elements coming from, how will they be used in the loop, etc. It would help us come up with the most relevant suggestion.

Here is a smallish example of another approach, although there are a number more I could propose.

@if "%BATCH_DEBUG%"=="" echo off

setLocal EnableDelayedExpansion

set LIST_ITEMS=ITEM1 ITEM2 ITEM3 ITEM4
set OTHER_ITEMS=TEST1 TEST2 TEST3 TEST4

set i=0
for %%x in (%LIST_ITEMS%) do (
  set /a i+=1
  call :GetElement OTHER_ITEMS !i! Item
    echo %%x !Item!
  )
)

EXIT /B

:GetElement [list-name] [element-number] [return-variable-name]
  for /f "tokens=%~2" %%a in ("!%~1!") do set %~3=%%a
  exit /b

Open in new window

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39668737
Here's another approach using a sort of two dimensional array model.

@if "%BATCH_DEBUG%"=="" echo off

setLocal EnableDelayedExpansion

set LIST_ITEMS="ITEM1,TEST1","ITEM2,TEST2","ITEM3,TEST3","ITEM4,TEST4"

for %%x in (%LIST_ITEMS%) do (
  for /f "tokens=1-2 delims=," %%a in (%%x) do (
    echo %%a %%b
  )
)

exit /B

Open in new window

~bp
0
 

Author Closing Comment

by:atomicgs12
ID: 39669448
I gave out the points mostly to 'dragon-it' for coming up with the most direct solution first but also gave points to 'bp' for solutions that work and that I can keep in my repertoire of cold snippets. Thanks to all.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39669597
No problem, glad it helped.  As with all programming there is milion different ways of solving the same problem, some more elegant, some more "right", some more scalable etc. but depends on the problem as you know.

Good luck with it

Steve
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now