批处理文件 - 纠错码失败

时间:2017-10-12 16:24:17

标签: batch-file

我尝试将错误更正添加到批处理文件中。我正在尝试以下方法:

set "building="
set LANbuilding[1]=20
set LANbuilding[2]=21
set LANbuilding[3]=22
set LANbuilding[4]=23
set LANbuilding[5]=24
set LANbuilding[6]=25

:start 

set /p building="Enter the building:> "

for /l %%n in (1,1,6) do ( if %building% neq !LANbuilding[%%n]! 
echo invalid building, try again. 

goto :start ) 

else

" else"将继续其余的代码。

到目前为止,这不会产生任何错误,只是关闭窗口。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

当您期望从已知值列表中进行选择时,请不要使用Set/P

Choice /C 123456 /N /M "Enter the building: "
Set "building=%ERRORLEVEL%"

修改

您甚至可以将其扩展为执行其他操作,例如将变量和值附加到选择中:

@Echo Off

Set "LANbuilding[1]=20"
Set "LANbuilding[2]=21"
Set "LANbuilding[3]=22"
Set "LANbuilding[4]=23"
Set "LANbuilding[5]=24"
Set "LANbuilding[6]=25"

Choice /C 123456 /N /M "Enter the building: "
Call Echo %%%%LANbuilding[%%ERRORLEVEL%%]%%%%=%%LANbuilding[%ERRORLEVEL%]%%
Timeout 5 /NoBreak>Nul

<强> EDIT2

如果您已经在使用延迟扩展,那么您应该能够直接回显这样的值:

Set "LANbuilding[1]=20"
Set "LANbuilding[2]=21"
Set "LANbuilding[3]=22"
Set "LANbuilding[4]=23"
Set "LANbuilding[5]=24"
Set "LANbuilding[6]=25"

Choice /C 123456 /N /M "Enter the building: "
Echo the value associated with LANbuilding[%ERRORLEVEL%] is !LANbuilding[%ERRORLEVEL%]!
Timeout 5 /NoBreak>Nul

<强> EDIT3

这使用Set/PFor循环,应该允许最终用户仅选择建筑物,20-25:

Set "LANbuilding[1]=20"
Set "LANbuilding[2]=21"
Set "LANbuilding[3]=22"
Set "LANbuilding[4]=23"
Set "LANbuilding[5]=24"
Set "LANbuilding[6]=25"

:Start 
Set/P "building=Enter the building: "
Set "_="
For /F "Tokens=1* Delims==" %%A In ('Set LANbuilding[ 2^>Nul'
) Do If "%building%" Equ "%%B" Set "_=%%A"
If Not Defined _ Echo invalid building, try again.&GoTo Start
Rem rest of code here:
Echo=[%%building%%=%building%]
Timeout 5 /NoBreak>Nul

在代码中,我还将变量%_%设置为LANBuilding [Number],其值也匹配%building%