Solved

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

Posted on 2013-11-21
8
3,947 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 52

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

895 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

15 Experts available now in Live!

Get 1:1 Help Now