0

I wish to have the for loop in my batch file to iterate 2 arrays. The pseudo code in my mind is something like below:

for each i in array1 
 print i
 print array2[x++]

array1 and array2 will have the same size.

Will I be able to achieve the same result in batch file? I currently have the following code.

for %%i in %APP_LIST1% DO (
    %appcmd% add app /site.name:%siteName% /path:/%%i /physicalPath:"d:\Apps\%%i"
)

I would like to use %APP_LIST2% (aka array2) in the same for loop as well.

Help please!

Kyle
  • 915
  • 8
  • 18
  • 34

1 Answers1

1

I am afraid I don't really understand what is your concern about the second array. If you can access one array, you can access any number of arrays in the same way...

Please, note that an array is a "collection of data items that can be selected by indices computed at run-time" as defined in Wikipedia. This way, you may use the same index used in the first array to access the second one; this should work in your case because "array1 and array2 will have the same size".

For example:

@echo off
setlocal EnableDelayedExpansion

rem Create first array
set i=0
for %%a in (apple orange pear) do (
   set /A i+=1
   set fruit[!i!]=%%a
)

rem Create second array
set i=0
for %%a in (red green blue) do (
   set /A i+=1
   set color[!i!]=%%a
)

rem Access both arrays at same time
for /L %%i in (1,1,3) do (
   echo Fruit: !fruit[%%i]!, color: !color[%%i]!
)

For further details on array management in Batch files, see: this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108