arrays - 批量循环菜单(多选)

标签 arrays loops batch-file menu batch-processing

我正在尝试设置一个批处理菜单,允许一次进行多项选择,然后运行所有功能。函数的顺序并不重要,只是函数运行时不会出现外部错误。这是我到目前为止的代码。

@Echo off
Echo Please Enter the corrasponding numbers separated by a space or colon (,) 
Echo for the Options that you would like to run e.g. 1 4,3 2
Echo Option #1 
Echo Option #2  
Echo Option #3  
Echo Option #4
Echo.

SET /P Selection=Please Select Restore Options? 
echo You chose: %Selection% 

setlocal ENABLEDELAYEDEXPANSION
Set /a index = 0
FOR %%A IN (%Selection%) DO (
SET Array[!index!] = %%A
SET /a index += 1
)

for /F "tokens=2 delims==" %%s in ('set Array[') DO (
set string=%%s
set string=%string: =%
echo %string%
Call :Opt%string%
)

pause
goto :EOF


:Opt1
ECHO Option 1's code
GOTO :EOF

:Opt2
ECHO Option 2's code
GOTO :EOF

:Opt3
ECHO Option 3's code
GOTO :EOF

:Opt4
ECHO Option 4's code
GOTO :EOF

我的代码可以工作到尝试调用 Array veriable 并将其附加到 Call 例如调用:Opt%%s 我遇到的问题是数组变量不断出现,并且在所选变量后面有一个空格。因此,我尝试使用 set string=%string:=% 来解决此问题,但我不断收到错误。

错误:

either echo is off and only opt is getting called with out the selected variable.

如果能提供帮助就太好了,提前致谢。

最佳答案

问题的开始是

SET Array[!index!] = %%A
------------------^-^---- = aditional spaces

使用了这些附加空格,因此您以名称中包含附加空格且值中包含附加空格的变量结尾。所以,更好地使用

SET "Array[!index!]=%%A"

出现 echo 错误的原因是您忘记在 for %%s 循环中使用延迟扩展。您更改循环内的 %string% 变量,并尝试在同一循环内使用更改后的值。

for /F "tokens=2 delims==" %%s in ('set Array[') DO (
    set "string=%%s"
    set "string=!string: =!"
    echo !string!
    Call :Opt!string!
)

但是原始集中中指出的更正使得无需替换空格。

关于arrays - 批量循环菜单(多选),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23843861/

相关文章:

python - 如何在 PyQt5 中制作终端控制台(终端模拟器)?

python - 从 Bat 文件调用 Python 并获取返回码

javascript - 如何将数组添加到数组对象

java - 尝试设置数组元素约束

arrays - typescript 数组接受错误的类型

bash - 在 bash for 循环中使用命令行参数范围打印包含参数的括号

php - 通过字符串键 x 访问数组,其中 x 为 "123"

ios - Swift iOS - 循环运行速度是否比 Date().timeIntervalSince1970 生成的日期秒快

c++ - 休息; C++ : which loop is it actually breaking

javascript - 如何从网站执行批处理文件