How to loop through array in batch?

Another Alternative using defined and a loop that doesn’t require delayed expansion:

set Arr[0]=apple
set Arr[1]=banana
set Arr[2]=cherry
set Arr[3]=donut

set "x=0"

:SymLoop
if defined Arr[%x%] (
    call echo %%Arr[%x%]%%
    set /a "x+=1"
    GOTO :SymLoop
)

Be sure you use “call echo” as echo won’t work unless you have delayedexpansion and use ! instead of %%

Leave a Comment