How to loop through array in batch?

batch array index
batch file loop through list
batch for loop
for loop in batch file
batch file multidimensional array
pass array to batch file
batch file loop through list of computers
batch variables

I created an array like this:

set sources[0]="\\sources\folder1\"
set sources[1]="\\sources\folder2\"
set sources[2]="\\sources\folder3\"
set sources[3]="\\sources\folder4\"

Now I want to iterate through this array:

for %%s in (%sources%) do echo %%s

It doesn't work! It seems that script is not going into the loop. Why is that? How can I iterate then?

If you don't know how many elements the array have (that seems is the case), you may use this method:

for /F "tokens=2 delims==" %%s in ('set sources[') do echo %%s

Note that the elements will be processed in alphabetical order, that is, if you have more than 9 (or 99, etc) elements, the index must have left zero(s) in elements 1..9 (or 1..99, etc.)

Batch Script - Arrays, Iterating Over an Array. Each element of the array needs to be specifically defined using the set command. The 'for' loop with the /L parameter for moving through ranges is used to iterate through the array. The following things need to be noted when arrays are implemented in Batch Script. Each element of the array needs to be defined with the set command. The ‘for’ loop would be required to iterate through the values of the array. Creating an Array. An array is created by using the following set command. set a[0]=1 Where 0 is the index of the array and 1 is the value assigned to the first element of the array.

Another Alternative using defined and a loop that doesn't require delayed expansion:

set Arr[0]=apple
set Arr[1]=banana
set Arr[2]=cherry
set Arr[3]=donut

set "x=0"

:SymLoop
if defined Arr[%x%] (
    call echo %%Arr[%x%]%%
    set /a "x+=1"
    GOTO :SymLoop
)

Be sure you use "call echo" as echo won't work unless you have delayedexpansion and use ! instead of %%

Arrays in Batch Scripting Language, 1- Array in Batch language; 2- Test the existance of an element; 3- Iterate through the All array elements need to be assigned values through a set command. How to Use Array in Windows Batch Programming?We have to define variables using set and the length has to defined first. The above script will print A, B, C in three lines and we treat obj[0], obj[1] and obj[2] as three individual variables (their memory address locations are not continuous). We can use set obj[%i%] to get strings for obj[0], obj[1], and obj[2] and we use the for to further evaluate the value.

If you don't need environment variables, do:

for %%s in ("\\sources\folder1\" "\\sources\folder2\" "\\sources\folder3\" "\\sources\folder4\") do echo %%s

whats the difference between: %%a and %variable% variables , files. If you need loop, then in most cases for has you covered. I tried looping through a directory of approx 80 files. I looped trough them and placed them into an Array. Then I did a nested loop through the array to find duplicated (case insensitive) dir names. But this is just as slow as looping straight through the dirs using For x in ./*.. Can someone with explain this?

This is one way:

@echo off
set sources[0]="\\sources\folder1\"
set sources[1]="\\sources\folder2\"
set sources[2]="\\sources\folder3\"
set sources[3]="\\sources\folder4\"

for /L %%a in (0,1,3) do call echo %%sources[%%a]%%

Populating Array in DOS Batch Script, From Jakash3's Blog, Arrays in Batch describes how to emulate arrays in the of the beauties of an array is the ability to populate an array with a loop, which is variable that :: :: for everytime it goes over it it is a :: :: new ID (Slot in the array). Arrays are usually used with loops. This is because it's (fairly) easy to access each array position in a loop - you just use the loop variable between the round brackets of the array. What we'll do now is to store some numbers in an array. We'll then use a loop to print them out to cells in a spreadsheet.

For posterity: I just wanted to propose a slight modification on @dss otherwise great answer.

In the current structure the way that the DEFINED check is done causes unexpected output when you assign the value from Arr to a temporary variable inside the loop:

Example:

@echo off
set Arr[0]=apple
set Arr[1]=banana
set Arr[2]=cherry
set Arr[3]=donut

set "x=0"

:SymLoop
if defined Arr[%x%] (
    call set VAL=%%Arr[%x%]%%
    echo %VAL%
    REM do stuff with VAL
    set /a "x+=1"
    GOTO :SymLoop
)

This actually produces the following incorrect output

donut
apple
banana
cherry

The last element is printed first. To fix this it is simpler to invert the DEFINED check to have it jump over the loop when we're done with the array instead of executing it. Like so:

@echo off
set Arr[0]=apple
set Arr[1]=banana
set Arr[2]=cherry
set Arr[3]=donut

set "x=0"

:SymLoop
if not defined Arr[%x%] goto :endLoop
call set VAL=echo %%Arr[%x%]%%
echo %VAL%
REM do your stuff VAL
SET /a "x+=1"
GOTO :SymLoop

:endLoop
echo "Done"

This regardless of what you do inside the SymLoop always produces the desired correct output of

apple
banana
cherry
donut
"Done"

More Powerful Batch Files Part II, Loops through a command using starting, stepping, and ending parameters indicated in the set. /F, Parses files or command output in a variety of ways. I will  Batch file for loop – looping through a range of values. In batch file programming, for loop can also be implemented through a range of values. Following is the syntax for implementing for loop through a range of values in the batch file. FOR /L %%var_name IN (Lowerlimit, Increment, Upperlimit) Do some_code. Where, /L signifies that for loop is used for iterating through a range of values

Windows batch: iterate over a static array, Windows batch: iterate over a static array - copies specified files. deploy.bat. @​echo off. set SRC=C:\some-path. set TARGET=\\REMOTE-HOST\other-path. I am trying to create a batch script for my Windows machine that loops through a list of (string/decimal) values and uses each value as a parameter inside the loop. Below is an example of a simple for loop I would like to use to display all the different version files (from my list)

Using Arrays in Batch Files, Now let's see if we can loop through all PROCESSOR_* variables, like they were elements in an array (in batch files, use %%A instead of %A,  32. The easiest way to do this is probably just to use the GroupBy method in LINQ: var batches = myEnumerable .Select( (x, i) => new { x, i }) .GroupBy(p => (p.i / 1000), (p, i) => p.x); But for a more sophisticated solution, see this blog post on how to create your own extension method to do this.

For - Loop through a range of numbers - Windows CMD, FOR /L. Conditionally perform a command for a range of numbers. Syntax FOR /L %%parameter IN (start,step,end) DO command Key start : The first number  To create your own arrays, I suggest using dots instead of underscores, and maybe prefix the name with a double underscore. The following command uses WMIC and a "Q&D" batch shortcut to store all properties of harddisk partition C: in an array-like set of environment variables, and uses FOR /F to loop through this array-like set of variables:

Comments
  • I like this way. Its bit simpler
  • This is definitely not helpful :/ I asked how to iterate through array, and you gave an answer: "just don't use the array!"
  • I only gave you this suggestion because sometimes I reconsider my program architecture depending on possible solutions. Imagine you had create array just to be able to iterate through items, then iterating without previous array would turn that array unneeded!
  • notice that space between in and ( is important. This wont work: in("\\sources...) but if you write with space in ("\\sources...) would work
  • @LS_dev Oh ye of little faith. Try it. :)
  • You're right, my apologies! As an aside, it would be nice to have a dynamic iteration regardless of array length.
  • The problem in your first example is simply the echo %var% change it to call echo %%var%% and it works as expected. Or even better, use delayed expansion
  • @jeb This is the crux of the issue, the code inside the loop doesn't behave as you would expect it in a normal bat script. You need to call things differently. This will be a maintenance pain in the long term.. better just make sure it works as users expect.
  • It behaves exactly as expected in a batch script! It's important to know, how batch works, than to circumvent constructs you don't understand. And you couldn't circumvent all parenthesis blocks.