I'm trying to create a menu selection with a Windows batch file.
When I use a for loop which creates variable i, and use it to call variable something[i] with !something[%%i]!
, it works perfectly.
However, when I try to create variable j from user input, and use it to call variable something[j] using !something[%%j]!
, it doesn't work.
I'm not sure why it's treating variable j differently to variable i, but it seems j can only be called by using !j!
rather than %%j
@echo off
setlocal EnableDelayedExpansion
set something[0]=aaaa
set something[1]=bbbb
set something[2]=cccc
set something[3]=dddd
set something[4]=eeee
set something[5]=ffff
set something[6]=gggg
for /l %%i in (0,1,6) do echo %%i. !something[%%i]!
set /p j="Input selection: "
echo.
echo j=%%j
echo j=!j!
echo.
set Selection=!something[%%j]!
echo Selection = !Selection!
pause
Here is a sample output:
0. aaaa
1. bbbb
2. cccc
3. dddd
4. eeee
5. ffff
6. gggg
Input selection: 3
j=%j
j=3
Selection =
Press any key to continue . . .