Skip to main content

Batch Script Arrays

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.

note

This requires defining each element of the array using the set command.

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
))
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.bat
@echo off 
set a[0]=1
echo %a[0]%
1

Consider the following more complex example:

ComplexAccessingExample.bat
@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]%
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.bat
@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]%
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.bat
@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]%
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.bat
@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]!
)
note
  • 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 above script produces the following output:

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.bat
@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%
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.bat
@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%%
)
note
  • Each variable defined using the set command has 2 values associated with each index of the array.
  • The variable i is set to 0 so that we can loop through the structure will the length of the array which is 3.
  • We always check for the condition on whether the value of i is equal to the value of len and if not, we loop through the code.
  • We are able to access each element of the structure using the obj[%i%] notation.

The above script produces the following output:

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