如何将用户从CHOICE命令输入的内容保存到变量中,以供以后在错误级别中使用

时间:2018-07-24 21:40:47

标签: batch-file dynamic choice

我有一个正在编写的程序的动态选择列表。我可以正常使用它,以便它可以基于CHOICE变量来更改count选项,但是现在我也在努力使errorlevel动态。这是我的代码:

SETLOCAL EnableDelayedExpansion
@ECHO off
SET count=7
SET ph=
FOR /L %%a IN (1,1,%count%) DO (
SET ph=!ph!%%a
ECHO !ph!
)
CHOICE /C Q%ph%
IF errorlevel (I don't have a variable for this) (
echo "in if" & pause
)

IF errorlevel 1 echo "out of if" & pause

我的想法是将错误级别设置为等于用户输入的错误级别(例如,用户输入7作为选择,错误级别变为7)。之所以这样做,是因为我需要errorlevel通过除1之外的所有内容,这是为退出选项保留的(这就是为什么我在其中使用“ Q”的原因)任何建议都非常感谢!谢谢!

2 个答案:

答案 0 :(得分:2)

要解决我的问题,我使用了NEQEQU语句,但我不知道它们可以与errorlevel一起使用。我还结合了@LotPings的建议来获取此代码,效果很好!为了从技术上将用户输入保存为变量,我只是从选择变量中减去一个,它等于相同值。

SETLOCAL EnableDelayedExpansion
REM @ECHO off
SET count=7
SET ph=
FOR /L %%a IN (1,1,%count%) DO (
SET ph=!ph!%%a
REM ECHO !ph!
)
CHOICE /C Q%ph%
set choice=%errorlevel%
ECHO %choice%
IF %choice% NEQ 1 ( 
ECHO "not equal" & PAUSE
SET /A choice=choice-1
ECHO !choice! REM This is now the value the user inputted.
PAUSE
)
IF %choice% EQU 1 ECHO "equal" & PAUSE

答案 1 :(得分:2)

I suggest following batch code for this task:

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
SET count=7
SET ph=
FOR /L %%a IN (1,1,%count%) DO SET ph=!ph!%%a
%SystemRoot%\System32\choice.exe /C Q%ph%
IF NOT ERRORLEVEL 2 ECHO Bye^^!& GOTO :EOF
SET /A UserChoice=%ERRORLEVEL%-1
ECHO You have chosen %UserChoice%.
PAUSE

IF NOT ERRORLEVEL 2 means if the exit code of CHOICE is NOT GREATER OR EQUAL 2 which is the same as if LESS THAN 2 then execute ECHO and GOTO to exit processing of this batch file.

The command IF does not modify value of ERRORLEVEL as documented at
What are the ERRORLEVEL values set by internal cmd.exe commands?

It would be also possible to first assign the exit code of CHOICE to an environment variable decremented by 1 and then make the comparison for quit by comparing the value with 0.

@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
SET count=7
SET ph=
FOR /L %%a IN (1,1,%count%) DO SET ph=!ph!%%a
%SystemRoot%\System32\choice.exe /C Q%ph%
SET /A UserChoice=%ERRORLEVEL%-1
IF %UserChoice% == 0 ECHO Bye^^!& GOTO :EOF
ECHO You have chosen %UserChoice%.
PAUSE

It is not advisable to use an environment variable with name choice as it makes it difficult to search for this environment variable in a batch file containing also external command CHOICE which is the reason for using UserChoice.

The command CHOICE is specified with full qualified file name (file path + file name + file extension) for safety reasons. Windows command processor does not need to search for choice.* with a file extension listed in environment variable PATHEXT in current directory and the directories listed in local environment variable PATH on using full qualified file name. This makes the batch file robust against corrupted system PATH containing the path of a folder before most important folder path %SystemRoot%\System32 which by chance contains also a choice.* file with a file extension listed in PATHEXT. The local environment variable PATH does not need to exist at all on running this batch file because of using full qualified file name of executable CHOICE. It also does not matter with full qualified file name if a user created a batch file with name choice.bat or choice.cmd in the directory being the current directory on running this batch file or any other directory in PATH being searched by cmd.exe before %SystemRoot%\System32.