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

Fig 1.5 Length of anArray

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.

Similar Reads

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....

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....

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’...

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....

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....

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....

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....

Contact Us