Solved

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

Posted on 2013-11-21
8
4,256 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 53

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 53

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 53

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

856 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