如何将这些变量与该选择相关联导致错误级别

时间:2018-10-19 00:37:15

标签: batch-file

我有一个目录%appdata%\lootbot\locations,其中可能有任意数量的文件,每个文件都包含一个目录路径。这段代码创建了这些(未知)文件的列表以及其中的路径以及选择提示,以及choice用于选择它们的数字。

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%)"

问题是选择提示后我不知道如何继续。我有errorlevel,但需要将其连接到正确的%dirname%,以便继续进行。我所有的想法都归结为将变量放入变量中,从而导致太多% ...我对此无能为力。

2 个答案:

答案 0 :(得分:1)

恐怕我不太了解您的要求...但是,也许这段代码可以为您提供帮助:

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%]!

我建议您阅读this post

答案 1 :(得分: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(不带参数)将设置chosen 到执行第一个for循环的undefined 显示菜单。 choices将是setchoice命令使用的整数字符串。

call :locations %errorlevel%会将chosen设置为 参数的值。执行第二个for循环。 如果chosencount的值相同, 然后将current设置为0,因为数字减去 相同的数字等于零。如果将零替换为零, 那么电流是不确定的,设置的标志 dirnamelocale

callchoicesdirnamelocale的末尾 将在非本地范围内设置。

dirnamelocale将在脚本结尾处回显 显示结果。