I have a rather confusing problem when using nested CALL functions and CHOICE commands inside of a batch file.
To summarize in pseudo code:
1.) Use a CHOICE command, which uses CALL :function1 when the correct option is selected
2.) :function1 uses CALL :setVar_n
3.) :setVar_n sets a list of variables, and ends with EXIT /B to return to :function1
4.) :function1 has a CHOICE command (Y/N), where Y will continue to perform operations then end with EXIT /B, and N ends with EXIT /B immediately
The issue:
The CHOICE command in :function1 always evaluates to N (the second option) regardless of input.
I don't understand why using %ERRORLEVEL% fails, while IF ERRORLEVEL works fine. I am also unsure why the use of CALL causes %ERRORLEVEL% to stop working in the first place.
I'm trying to avoid rewriting every choice command (There must be at least 50, some with 25+ options).
When it's written using %ERRORLEVEL% it fails:
::Return from setVar_n here
CHOICE /C YN /M "Continue? Y/N >"
IF %ERRORLEVEL%==2 (EXIT /B)
::function1 continues here
If I use IF ERRORLEVEL:
::Return from setVar_n here
CHOICE /C YN /M "Continue? Y/N >"
IF ERRORLEVEL 2 (EXIT /B)
::function1 continues here
It works properly. The issue is that the CHOICE problem persists even after :function1 ends. It affects all CHOICE commands in the entire file, so %ERRORLEVEL% cannot be used at all.
Can anyone shed some light on this issue?
Here's a full file code to test with, which might make more sense:
@ECHO OFF
:start
choice /c ABC
if %errorlevel%==1 (goto start)
if %errorlevel%==2 (call :function1)
if %errorlevel%==3 (goto start)
echo Function 1 completed
pause
choice /c ABC
if %errorlevel%==1 (echo 1)
if %errorlevel%==2 (echo 2)
if %errorlevel%==3 (echo 3)
pause
exit
:setVar
set /a var1=2
set /a var2=3
exit /b
:function1
echo In Function 1
call :setVar
choice /c YN /m "Continue (Y) or Finish (N)"
if %errorlevel%==2 (exit /b)
echo Still inside function 1
exit /b