batch file variable in variable name

January 14, 2011 at 13:22:09
Specs: windows xp professional
I am writing a program in which i need to have a variable in a variable name, but there are some catches. I can't figure it out. Please help.

@echo off
setlocal enabledelayedexpansion
set var1=1
set var2=2
set var3=3
set var4=4
set var5=5
for %%a in (1 3 2 4 5) do (
set /a counter+=1
echo %var!counter!%
)


See More: batch file variable in variable name

Report •


#1
January 14, 2011 at 14:51:10
@echo off
setlocal enabledelayedexpansion
set var1=1
set var2=2
set var3=3
set var4=4
set var5=5
for /L %%a in (1 1 5) do echo !var%%a!

Report •

#2
January 15, 2011 at 06:43:53
thanks IVO

Report •

#3
January 15, 2011 at 06:51:09
Can you also please help with this problem?


@echo off
setlocal enabledelayedexpansion
set var1=1
set var2=2
set var3=3
set var4=4
set var5=5
for /L %%a in (1 1 5) do (
echo !var%%a+1!
echo !var%%a!
)

the !var%%a+1! adds one to the number --> var# instead of var(#+1)


Report •

Related Solutions

#4
January 15, 2011 at 09:09:07
Due to the syntax required by dynamic variables and their constraints you need to code an internal subroutine (:SUB); the label :EOF doesn't need to be declared since it is the builtin return point for NT batch scripts, much like the Return or GoBack statement in programming languages.

@echo off
setlocal EnableDelayedExpansion
set var1=1
set var2=2
set var3=3
set var4=4
set var5=5
for /L %%a in (1 1 4) do  call :SUB %%a
goto :EOF

:SUB
  set count=%1
  set /A count+=1
  echo !var%count%!
  echo !var%1!
goto :EOF


Report •

#5
January 15, 2011 at 10:25:24
thanks so much. it works perfectly.

Report •


Ask Question