Professional Documents
Culture Documents
BatchScriptArrays
https://www.tutorialspoint.com/batch_script/batch_script_arrays.htm
Copyrighttutorialspoint.com
ArraysarenotspecificallydefinedasatypeinBatchScriptbutcanbeimplemented.Thefollowing
thingsneedtobenotedwhenarraysareimplementedinBatchScript.
Eachelementofthearrayneedstobedefinedwiththesetcommand.
Theforloopwouldberequiredtoiteratethroughthevaluesofthearray.
CreatinganArray
Anarrayiscreatedbyusingthefollowingsetcommand.
seta[0]=1
Where0istheindexofthearrayand1isthevalueassignedtothefirstelementofthearray.
Anotherwaytoimplementarraysistodefinealistofvaluesanditeratethroughthelistofvalues.The
followingexampleshowhowthiscanbeimplemented.
Example
@echooff
setlist=1234
(for%%ain(%list%)do(
echo%%a
))
Output
Theabovecommandproducesthefollowingoutput.
1
2
3
4
AccessingArrays
Youcanretrieveavaluefromthearraybyusingsubscriptsyntax,passingtheindexofthevalueyou
wanttoretrievewithinsquarebracketsimmediatelyafterthenameofthearray.
Example
@echooff
seta[0]=1
echo%a[0]%
Inthisexample,theindexstartsfrom0whichmeansthefirstelementcanbeaccessedusingindexas
https://www.tutorialspoint.com/cgibin/printpage.cgi 1/4
15/03/2017 BatchScriptArrays
0,thesecondelementcanbeaccessedusingindexas1andsoon.Let'scheckthefollowingexample
tocreate,initializeandaccessarrays
@echooff
seta[0]=1
seta[1]=2
seta[2]=3
echoThefirstelementofthearrayis%a[0]%
echoThesecondelementofthearrayis%a[1]%
echoThethirdelementofthearrayis%a[2]%
Theabovecommandproducesthefollowingoutput.
Thefirstelementofthearrayis1
Thesecondelementofthearrayis2
Thethirdelementofthearrayis3
ModifyinganArray
Toaddanelementtotheendofthearray,youcanusethesetelementalongwiththelastindexofthe
arrayelement.
Example
@echooff
seta[0]=1
seta[1]=2
seta[2]=3
RemAddinganelementattheendofanarray
Seta[3]=4
echoThelastelementofthearrayis%a[3]%
Theabovecommandproducesthefollowingoutput.
Thelastelementofthearrayis4
YoucanmodifyanexistingelementofanArraybyassigninganewvalueatagivenindexasshown
inthefollowingexample
@echooff
seta[0]=1
seta[1]=2
seta[2]=3
RemSettingthenewvalueforthesecondelementofthearray
Seta[1]=5
echoThenewvalueofthesecondelementofthearrayis%a[1]%
Theabovecommandproducesthefollowingoutput.
Thenewvalueofthesecondelementofthearrayis5
IteratingOveranArray
Iteratingoveranarrayisachievedbyusingtheforloopandgoingthrougheachelementofthe
array.Thefollowingexampleshowsasimplewaythatanarraycanbeimplemented.
@echooff
setlocalenabledelayedexpansion
https://www.tutorialspoint.com/cgibin/printpage.cgi 2/4
15/03/2017 BatchScriptArrays
settopic[0]=comments
settopic[1]=variables
settopic[2]=Arrays
settopic[3]=Decisionmaking
settopic[4]=Timeanddate
settopic[5]=Operators
for/l%%nin(0,1,5)do(
echo!topic[%%n]!
)
Followingthingsneedtobenotedabouttheaboveprogram
Eachelementofthearrayneedstobespecificallydefinedusingthesetcommand.
Theforloopwiththe/Lparameterformovingthroughrangesisusedtoiteratethroughthe
array.
Output
Theabovecommandproducesthefollowingoutput.
Comments
variables
Arrays
Decisionmaking
Timeanddate
Operators
LengthofanArray
Thelengthofanarrayisdonebyiteratingoverthelistofvaluesinthearraysincethereisnodirect
functiontodeterminethenumberofelementsinanarray.
@echooff
setArr[0]=1
setArr[1]=2
setArr[2]=3
setArr[3]=4
set"x=0"
:SymLoop
ifdefinedArr[%x%](
callecho%%Arr[%x%]%%
set/a"x+=1"
GOTO:SymLoop
)
echo"Thelengthofthearrayis"%x%
Output
OutputTheabovecommandproducesthefollowingoutput.
Thelengthofthearrayis4
CreatingStructuresinArrays
Structurescanalsobeimplementedinbatchfilesusingalittlebitofanextracodingfor
https://www.tutorialspoint.com/cgibin/printpage.cgi 3/4
15/03/2017 BatchScriptArrays
implementation.Thefollowingexampleshowshowthiscanbeachieved.
Example
@echooff
setlen=3
setobj[0].Name=Joe
setobj[0].ID=1
setobj[1].Name=Mark
setobj[1].ID=2
setobj[2].Name=Mohan
setobj[2].ID=3
seti=0
:loop
if%i%equ%len%goto:eof
setcur.Name=
setcur.ID=
for/f"usebackqdelims==.tokens=13"%%jin(`setobj[%i%]`)do(
setcur.%%k=%%l
)
echoName=%cur.Name%
echoValue=%cur.ID%
set/ai=%i%+1
gotoloop
Thefollowingkeythingsneedtobenotedabouttheabovecode.
Eachvariabledefinedusingthesetcommandhas2valuesassociatedwitheachindexofthe
array.
Thevariableiissetto0sothatwecanloopthroughthestructurewillthelengthofthearray
whichis3.
Wealwayscheckfortheconditiononwhetherthevalueofiisequaltothevalueoflenandif
not,weloopthroughthecode.
Weareabletoaccesseachelementofthestructureusingtheobj[%i%]notation.
Output
Theabovecommandproducesthefollowingoutput.
Name=Joe
Value=1
Name=Mark
Value=2
Name=Mohan
Value=3
https://www.tutorialspoint.com/cgibin/printpage.cgi 4/4