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

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
atomicgs12Asked:
Who is Participating?
 
Steve KnightIT ConsultancyCommented:
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
 
Bill PrewCommented:
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
 
atomicgs12Author Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Steve KnightIT ConsultancyCommented:
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
 
Bill PrewCommented:
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
 
Bill PrewCommented:
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
 
atomicgs12Author Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.