Solved

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

Posted on 2013-11-21
8
4,544 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 54

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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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
 
LVL 54

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 54

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

710 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