I have an array that contains all of my GOTO tag names. I want to be able to use a variable as the array index and use that to jump to the specific part of my code. My code will prompt the user for an input and based on the screen they are on (%room%) and the input they give it will will jump to a specific point in my code.
I am fairly new to batch and most of my code is modified from the internet. in the folder that has this file I have another folder called 'Arrays'. In that folder I have a bunch of text files that contain my Array elements. My arrays are populated using these text files and the name of the file becomes the name of the array.
Here is my code
@Echo Off
set /A room=1
::--part a (populating arrays) ----------------------
set "file=%~dp0Arrays"
For /R %file% %%G IN (*.txt) DO (
set /A i=0
for /F "usebackq delims=" %%a in ("%%G") do (
set /A i+=1
call set %%~nG[%%i%%]=%%a
call set n=%%i%%
)
)
::--part b (populating arrays) ----------------------
echo %%ajump[%room%]%%
:: returns %roomtext[1]%
call echo|set /p=%%ajump[%room%]%%
:: returns dha
echo.
::--part c (jumping to point in code using array)-----
SET /P choice= Type your choice:
if /I %choice%==a (goto %%ajump[%room%]%%)
exit
::--part d (code to jump to) ----------------------
:dha
ehco succsess
pause
exit
I populate my arrays using part a
I then use part c to jump to the specific part in the code.
However when I use run this I get "The system cannot find the batch label specified - %ajump[1]%" (omit the quotes)
and if I change the jump code to (remove a set of %)
if /I %choice%==a (goto %ajump[%room%]%)
I get "The system cannot find the batch label specified - room" (omit the quotes)
ajump[1] reads dha
Part b concerns me because when I tire to just echo it, it echos the same value that it tries to jump to. however when I use the call set|echo /p= it echos the actual contents of the array.
I can echo all of the contents for all of the arrays in the folder 'Arrays' using this code
For /R %file% %%F IN (*.txt) DO (
echo %%%%~nF
for /L %%i in (1,1,10) do call echo %%%%~nF[%%i]%%
echo.
)
Is my if statement wrong or could it be my jump label. Since it echos the same text as where it tries to jump I am lead to believe that it is the way I am calling the arrays that is the problem.