I have a dynamic list of choices for a program I'm writing. I have it working correctly so that it will change the CHOICE options based on the count variable but now I'm struggling with making the errorlevel dynamic as well. Here is my code:
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
My idea is to set the errorlevel equal to what the user put in (e.g. the user puts in 7 as their choice, the errorlevel becomes 7) The reason I want to do this is because I need the errorlevel to pass for everything besides 1, which is reserved for a quit option (which is why I have the "Q" there) Any advice and suggestions are much appreciated! Thanks!
I suggest following batch code for this task:
IF NOT ERRORLEVEL 2means 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
ERRORLEVELas documented atWhat 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.It is not advisable to use an environment variable with name
choiceas it makes it difficult to search for this environment variable in a batch file containing also external command CHOICE which is the reason for usingUserChoice.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 variablePATHEXTin current directory and the directories listed in local environment variablePATHon using full qualified file name. This makes the batch file robust against corrupted systemPATHcontaining the path of a folder before most important folder path%SystemRoot%\System32which by chance contains also achoice.*file with a file extension listed inPATHEXT. The local environment variablePATHdoes 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 namechoice.batorchoice.cmdin the directory being the current directory on running this batch file or any other directory inPATHbeing searched bycmd.exebefore%SystemRoot%\System32.