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