It is possible to create a set of variables that can act similar to an array (although they are not an actual array object) by using spaces in the SET
statement:
@echo off
SET var=A "foo bar" 123
for %%a in (%var%) do (
echo %%a
)
echo Get the variable directly: %var%
Result:
A
"foo bar"
123
Get the variable directly: A "foo bar" 123
It is also possible to declare your variable using indexes so you may retrieve specific information. This will create multiple variables, with the illusion of an array:
@echo off
setlocal enabledelayedexpansion
SET var[0]=A
SET var[1]=foo bar
SET var[2]=123
for %%a in (0,1,2) do (
echo !var[%%a]!
)
echo Get one of the variables directly: %var[1]%
Result:
A
foo bar
123
Get one of the variables directly: foo bar
Note that in the example above, you cannot reference var
without stating what the desired index is, because var
does not exist in its own. This example also uses setlocal enabledelayedexpansion
in conjunction with the exclamation points at !var[%%a]!
. You can view more information about this in the Variable Substitution Scope Documentation.