Batch Script – Arrays
An array is a collection of elements of the same data type. The arrays are not explicitly defined as Batch Script types but can be used. The following items need to be noted when the same members are used in Batch Script.
- Each aspect of the same members needs to be defined by a set order.
- A ‘for’ loop will be required to double the program values.
How to create an array?
We can create an array using the command ‘set’. For example, set a[0] = 1 means we created an array whose first element of index 0 has a value of 1. Also, another way to define an array is by creating a list and then looping through it. Let us look at the below example.
@echo off set list_1=7 9 1 3 (for %%p in (%list_1%) do ( echo %%p ))
The above command produces the below output
7 9 1 3
How to Access an Array and its elements?
The easiest way to access an array is by using its Index. In the above example, the first element can be accessed as a[0], second with a[1], and so on. The index starts from 0 and ends with length(array)-1. Let’s look at the below example, where we have 2 elements in the array and we are accessing the values by their index.
@echo off set a[0]=5 set a[1]=12 echo The first element of the array created is %a[0]% echo The second element of the array created is %a[1]%
The Above code will produce the following output.
The first element of the array created is 5 The second element of the array created is 12
How to modify the elements of an array?
We can add a new element or modify the value of an existing element by using its index. The output of the below program is ‘last element is 12’
@echo off set a[0]=5 set a[1]=7 set a[2]=9 Rem Adding a new element at the array end Set a[3]=12 echo The last element is %a[3]%
Now let us look at modifying the existing value. In the below snippet, we assign a[2] as 14 and the third element of the array is now 14
Set a[2]=14
echo The new value of the third element of the array is %a[2]%
The output of the above snippet results in the below behavior
The new value of the third element of the array is 14
Iterating over an Array
Iterating over an array is achieved using for loop. Using for loop, we can access elements one by one. Let us look at the example below. /l is used here, as we move through the specified range and iterate through the Array.
@echo off set a[0]=5 set a[1]=7 set a[2]=9 Rem Adding a new element at the array end Set a[3]=12 echo The last element is %a[3]%
The Above snippets produce the below Output
Download Upload Browse Add Save Delete
Length of an Array
There is no predefined function to find the length of the array. We can use for loop and Iterate through the Array.
@echo off :: Here an array is defined set array[0]=1 set array[1]=4 set array[2]=9 set array[3]=10 :: Here we initializing an variable named :: len to calculate length of array set len=0 :: To iterate the element of array :Loop :: It will check if the element is defined or not if defined array[%len%] ( set /a len+=1 GOTO :Loop ) echo The length of the array is %len% pause
The above program produces the below output.
The length of the array is 4
Creating Structures inside Arrays
We can also create structures inside an array. In the below example, Each of the variables defined using the default command (set) has 2 values associated with each of the identical members. The variable i is set to 0 so that we can move through the structure and the loop will run 3 times. We constantly check the status of whether i value is equal to len value and if not, we enter with code. We can access each component of the structure using obj [%i%] notation.
@echo off set obj[0].Name=Akash set obj[0].ID=101 set obj[1].Name=Ajay set obj[1].ID=102 set obj[2].Name=Thomas set obj[2].ID=103 FOR /L %%i IN (0 1 2) DO ( call echo User Name = %%obj[%%i].Name%% call echo User ID = %%obj[%%i].ID%% )
The above code produces the below output
User Name = Akash User ID = 101 User Name = Ajay User ID = 102 User Name = Thomas User ID = 103
Test Existence of an element
We use the if defined functionality to check if an element is in the array or not. %1 is the first argument from the invoking command line. %~1 means the quotes surrounding the arguments are removed.
@ECHO OFF &SETLOCAL set "Array[Akash]=true" set "Array[John]=true" set "Array[Venky]=true" set "Array[Praveen]=true" set "MyUserName=Akash" call:check "%MyUserName%" set "MyUserName=Paul" call:check "%MyUserName%" goto:eof :check if defined Array[%~1] ( echo %~1 is in the array. ) else ( echo %~1 is NOT in the array. ) exit /b
The above code gives the below output
Akash is in the array. Paul is NOT in the array.
Contact Us