Reputation: 127
I ran into a problem in a bigger batch file I was making, and narrowed it down to a very particular problem. If I manually set the errorlevel like this: set errorlevel=5
, then the "choice" command can't set or override my errorlevel. How can I get past this from happening?
I made a batch file to test this out. Here it is:
@echo off
set errorlevel=5
choice /c 123
echo %errorlevel%
pause
And the output, if you were to press 2:
[1,2,3]?2
5
Press any key to continue . . .
Upvotes: 0
Views: 540
Reputation: 67196
I used to use a simple subroutine to set the errorlevel to any value:
@echo off
call :errorlevel=5
echo %errorlevel%
goto :EOF
:errorlevel
exit /B %1
Upvotes: 2
Reputation: 41224
System environment variables can be used by the batch file writer, but that is a really bad idea.
PATH TEMP WINDIR USERNAME USERPROFILE ERRORLEVEL TIME DATE
are some of variable names you should avoid using. Type SET
at a cmd prompt to see the usual ones that are in use, but it doesn't show them all.
Choice is operating normally, and other tools will fail to set an errorlevel too.
Upvotes: 1
Reputation: 2688
use cmd /c exit /b 5
instead of set errorlevel=5
like this:
@echo off
cmd /c exit /b 5
choice /c 123
echo %errorlevel%
pause
Upvotes: 1