Professional Documents
Culture Documents
Arrays in Batch Scripting Language
Arrays in Batch Scripting Language
MENU
For other languages an array has fixed size. But in the Batch language, the array has a dynamic size, and there is no attribute
describing array length (the number of array elements). And no function directly helps you get the number of array elements.
All array elements need to be assigned values through a set command. If not, such element doesn't exist .
** syntax **
?
1 @echo off
2
3 set myarray[0] = Abc
4 set /A myarray[1] = 234
5 set myarray[2]=Def
6
7 set myarray[0]=A new value
8
Example:
arrayExample1.bat
?
1 @echo off
2
3 set names[0]=Tom
4 set names[1]=Jerry
5 set names[2]=Donald
6 set names[3]=Aladin
7
8 echo names[0]= %names[0]%
9 echo names[3]= %names[3]%
10
11 @rem names[10] does not exists!
https://o7planning.org/en/11601/arrays-in-batch-scripting-language 1/6
6/23/2020 Arrays in Batch Scripting Language
12 echo names[10]= %names[10]%
13
14 pause
arrayExample2.bat
?
1 @echo off
2
3 set names[0]=Tom
4 set names[1]=Jerry
5
6 echo names[0]= %names[0]%
7
8 @rem: Assign new value
9 set names[0]=Donald
10
11 echo After assign new value to names[0]:
12 echo names[0]= %names[0]%
13
14 pause
arrayDefinedExample.bat
?
1 @echo off
2 set Arr[0]=1000
3 set Arr[1]=5000
4 set Arr[2]=3000
5
6 if not defined Arr[5] (
7
8 echo Element at 5 does not exists!
9 )
10
11 if defined Arr[1] (
12
https://o7planning.org/en/11601/arrays-in-batch-scripting-language 2/6
6/23/2020 Arrays in Batch Scripting Language
13 echo Element at 1 exists!
14 )
15
16 pause
“ See more:
For /F loop can be approved on a range of numbers, therefore, it can be approved on a range of array indices
fetchArrayExample1.bat
?
1 @echo off
2
3 set fruits[0]=Apple
4 set fruits[1]=Apricot
5 set fruits[2]=Asparagus
6 set fruits[3]=Aubergine
7 set fruits[4]=Banana
8
9 FOR /L %%i IN (0 1 4) DO (
10
11 call echo Element At %%i = %%fruits[%%i]%%
12 )
13
14 pause
If you don't know the number of array elements in advance, you can iterate over its elements by using goto command.
fetchArrayExample2.bat
?
1 @echo off
2
https://o7planning.org/en/11601/arrays-in-batch-scripting-language 3/6
6/23/2020 Arrays in Batch Scripting Language
3 set fruits[0]=Apple
4 set fruits[1]=Apricot
5 set fruits[2]=Asparagus
6 set fruits[3]=Aubergine
7 set fruits[4]=Banana
8
9
10 set /A i = 0
11
12 :my_loop
13
14 if defined fruits[%i%] (
15
16 call echo Element At %i% = %%fruits[%i%]%%
17
18 set /a i = %i% + 1
19
20 goto :my_loop
21
22 )
23
24 echo Done!
25
26 pause
structureArrayExample.bat
?
1 @echo off
2
3 set persons[0].firstName=Bill
4 set persons[0].lastName=Gates
5
6 set persons[1].firstName=Steve
7 set persons[1].lastName=Jobs
8
9 set persons[2].firstName=Mark
10 set persons[2].lastName=Zuckerberg
11
12 set persons[3].firstName=Sundar
13 set persons[3].lastName=Pichai
14
15 FOR /L %%i IN (0 1 3) DO (
16
17 call echo Person At %%i = %%persons[%%i].firstName%% %%persons[%%i].lastName%%
18 )
19
20 pause
https://o7planning.org/en/11601/arrays-in-batch-scripting-language 4/6
6/23/2020 Arrays in Batch Scripting Language
Newest Documents
Android CharacterPickerDialog
Android Dialog
Android TimePickerDialog
Create a simple File Chooser in Android
Create a simple File Dialog Finder in Android
Android Wifi Scanning
Upgrade Mac Operating System
How do I take a MacOS Retina screenshot and get the image at its actual size?
iOS Tutorial for Beginners - Hello iOS
Android Phone Call
o7planning.org
https://o7planning.org/en/11601/arrays-in-batch-scripting-language 5/6
6/23/2020 Arrays in Batch Scripting Language
https://o7planning.org/en/11601/arrays-in-batch-scripting-language 6/6