Skip to content

Batch Script Arrays

How to implement and work with arrays in Windows Batch Script using the set command and for loops for element definition, access, modification, iteration, and length calculation.

Batch Script Arrays

Arrays do not exist as a type in Batch Script, but it can be implemented using the set command ad a for loop:

  • 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

You can create an array 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 way to implement arrays is to define a list of values and iterate through the list of values:

@echo off 
set list=1 2 3 4 
(for %%a in (%list%) do ( 
   echo %%a 
))
Outputplaintext
1
2
3
4

Accessing Arrays

You can retrieve a value from the array by passing the index of the value you want to retrieve in square brackets immediately after the name of the array.

AccessingExample.batbatch
@echo off 
set a[0]=1 
echo %a[0]%
Outputplaintext
1

Consider the following more complex example:

ComplexAccessingExample.batbatch
@echo off
set a[0]=1 
set a[1]=2 
set a[2]=3 
echo The first element of the array is %a[0]% 
echo The second element of the array is %a[1]% 
echo The third element of the array is %a[2]%
Outputplaintext
The first element of the array is 1
The second element of the array is 2
The third element of the array is 3

Modifying an Array

To add an element to the end of the array, you can use the set command along with the last index of the array element:

AddLastElement.batbatch
@echo off 
set a[0]=1  
set a[1]=2  
set a[2]=3 
Rem Adding an element at the end of an array 
Set a[3]=4 
echo The last element of the array is %a[3]%
Outputplaintext
The last element of the array is 4

You can modify an existing element of an Array by assigning a new value at a given index using the set command:

ModifyLastElement.batbatch
@echo off 
set a[0]=1 
set a[1]=2  
set a[2]=3 
Rem Setting the new value for the second element of the array 
Set a[1]=5 
echo The new value of the second element of the array is %a[1]%
Outputplaintext
The new value of the second element of the array is 5

Iterating Over an Array

Iteration of an array is achieved by using the for loop and reviewing each element of the array.

An example of how to implement iteration on an array:

Iteration.batbatch
@echo off 
setlocal enabledelayedexpansion 
set fruit[0]=banana
set fruit[1]=apple
set fruit[2]=pear
set fruit[3]=orange
set fruit[4]=peach
set fruit[5]=watermelon 

for /l %%n in (0,1,5) do ( 
   echo !fruit[%%n]! 
)

The above script produces the following output:

Outputplaintext
banana
apple
pear
orange
peach
watermelon

Length of an Array

The length of an array is done by iterating over the list of values in the array since there is no direct function to determine the number of elements in an array.

LengthOfArray.batbatch
@echo off 
set Arr[0]=1 
set Arr[1]=2 
set Arr[2]=3 
set Arr[3]=4 
set "x=0" 
:SymLoop 

if defined Arr[%x%] ( 
   call echo %%Arr[%x%]%% 
   set /a "x+=1"
   GOTO :SymLoop 
)
echo "The length of the array is" %x%
Outputplaintext
1
2
3
4
"The length of the array is" 4

Creating Structures in Arrays

Structures can also be implemented in batch files, using a little extra code for implementation.

StructureExample.batbatch
@echo off 
set obj[0].Name=Tom 
set obj[0].ID=1 
set obj[1].Name=Ryan 
set obj[1].ID=2 
set obj[2].Name=John 
set obj[2].ID=3 
FOR /L %%i IN (0 1 2) DO  (
   call echo Name = %%obj[%%i].Name%%
   call echo Value = %%obj[%%i].ID%%
)

The above script produces the following output:

Outputplaintext
Name = Tom
Value = 1
Name = Ryan
Value = 2
Name = John
Value = 3
Navigation