0

I have the name of a variable stored inside another variable and i would to find the value of variable the first variable.

I wrote this code to solve it.

set var1=aaa
set var2=bbb
set var3=ccc
set var4=ddd
echo pick number 1 to 4
set /p ReturnCode=
set answer=%%var_%ReturnCode%%%
echo %answer%

My trouble is it gives name of variable back instead of value.

For example, if i enter 3 to chose instead of outputting 'ccc', it outputs 'var_3'. I would appreciate any help you could give, Thanks.

maximuslotr
  • 74
  • 1
  • 1
  • 9

1 Answers1

2

2 ways to do it:

@echo off
setlocal enabledelayedexpansion
set var1=aaa
set var2=bbb
set var3=ccc
set var4=ddd
echo pick number 1 to 4
set /p ReturnCode=

:: using delayed variable expansion
set answer=!var%ReturnCode%!
echo %answer%

:: using the call double percent trick
call echo %%var%ReturnCode%%%

:: or
call set answer2=%%var%ReturnCode%%%
echo %answer2%
avery_larry
  • 2,069
  • 1
  • 5
  • 17