-1

I have a directory %appdata%\lootbot\locations where there might be any number of files, each of which contain a single directory path. this code creates a list of those (unknown) files and the paths within and a choice prompt, along with a number to be used by choice for selecting them.

set "count=0"
for %%s in ("%appdata%\lootbot\locations\*") do set /p locale=<"%%~s" & set "dirname=%%~s" & call :recall
set "choices="
if defined string1 echo %string1% & set "choices=1"
if defined string2 echo %string2% & set "choices=12"
if defined string3 echo %string3% & set "choices=123"
if defined string4 echo %string4% & set "choices=1234"
if defined string5 echo %string5% & set "choices=12345"
if defined string6 echo %string6% & set "choices=123456"
if defined string7 echo %string7% & set "choices=1234567"
if defined string8 echo %string8% & set "choices=12345678"
if defined string9 echo %string9% & set "choices=123456789"
if not defined choices echo too many & pause & exit /b
choice /c %choices%q /n /m "select the saved directory number you want to re-enter or Q to quit: "
echo errorlevel %errorlevel%
exit /b

:recall
set /a "count+=1"
set "string%count%=%count%: %dirname% (%locale%)"

the problem is that I can't figure out how to proceed after the choice prompt. I have the errorlevel but I need to connect that to the correct %dirname% so I can proceed. all my ideas come down to putting variables within variables, resulting in too many %... I can't wrap my mind around this.

Bricktop
  • 533
  • 3
  • 22

2 Answers2

1

I am afraid I don't really understand what is your request... However, perhaps this code may help you:

setlocal EnableDelayedExpansion

. . .

set "choices= 123456789ABCDEFGHIJKLMNOPQRSTUVW" & rem Max = 32

set "count=0"
for %%s in ("%appdata%\lootbot\locations\*") do (
   set /A count+=1
   if !count! gtr 32 echo Too many & pause & exit /B
   for /F %%c in ("!count!") do (
      set /P "locale[%%c]=" < "%%s"
      set "dirname[%%c]=%%~s"
      echo !choices:~%%c,1!: !dirname[%%c]! (!locale[%%c]!^)
   )
)

choice /c !choices:~1,%count%!X /n /m "select the saved directory you want to re-enter or X to eXit: "
echo errorlevel: %errorlevel%
echo Selected dirname: !dirname[%errorlevel%]!
echo Selected locale:  !locale[%errorlevel%]!

I suggest you to read this post.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • good stuff. I'm trying to figure out how to use the `X` for exit. because the variable is the last one the errorlevel number varies. putting it in the front would mess up the numbering. – Bricktop Oct 19 '18 at 20:10
1
call :locations

rem Too many files causes error.
if errorlevel 1 exit /b 1

rem Probably no files if choices is undefined.
if not defined choices exit /b 2

choice /c %choices%q /n /m "select the saved directory number you want to re-enter or Q to quit: "
call :locations %errorlevel%

rem Probably entered q if dirname is undefined.
if not defined dirname exit /b 0

echo dirname is "%dirname%".
echo locale is "%locale%".
exit /b

:locations
setlocal
set "count=0"
set "chosen=%~1"

if not defined chosen (
    rem Display menu and set choices.

    for %%s in ("%appdata%\lootbot\locations\*") do (
        set /a "count+=1"
        set /p locale=<"%%~s"
        call echo %%count%%: %%~s (%%locale%%^)
        call set "choices=%%choices%%%%count%%"
    )

    set "locale="
) else (
    rem Set dirname and locale.

    for %%s in ("%appdata%\lootbot\locations\*") do (
        set /a "count+=1"

        @rem If current is 0, then replace 0 with nothing makes current undefined.
        set /a "current=chosen-count"
        call set "current=%%current:0=%%"

        if not defined current (
            set /p locale=<"%%~s"
            set "dirname=%%~s"
        )
    )
)

if %count% gtr 9 exit /b 1

rem Set nonlocal variables.
endlocal & (
    set "choices=%choices%"
    set "dirname=%dirname%"
    set "locale=%locale%"
)
exit /b

call :locations with no arguments will set chosen to undefined which executes the 1st for loop to display the menu. choices will be set with the string of integers to be used with the choice command.

call :locations %errorlevel% will set chosen to the value of the argument. The 2nd for loop is executed. If chosen is the same value of count, then current will be set to 0 as the number minus the same number equals zero. If zero is replaced with zero, then current is undefined, which flags for setting dirname and locale.

At the end of the call, choices, dirname and locale will be set in nonlocal scope.

dirname and locale will be echoed at end of the script to display the result.

michael_heath
  • 5,262
  • 2
  • 12
  • 22