Is ERRORLEVEL reliable?

Since ERRORLEVEL is an environment variable, is it possible that its value can be changed before I get the opportunity to check it in my batch file?

+3
source share
2 answers

Environment variables refer to the current "process", so they cannot be changed externally. If you check the error level after the appropriate command in your batch file, you must check the correct value.

You can confirm this by opening two command windows and entering the first:

c:> set errorlevel=7

and then the second:

c:> set errorlevel=9

then go back to the first and:

c:> echo %errorlevel%
7

errorlevel. , , .

, - if errorlevel N ( ), set errorlevel= .

+2

%errorlevel% ( ) . .

, , set errorlevel=15, , set, reset %errorlevel%, nowetete 0.

"" %errorlevel% if errorlevel:

if errorlevel 0 do somecommand
if errorlevel 1 do somethingelse
...
if errorlevel 255 do lastalternative

. % errorlevel% , -

if errorlevel 0 do set myerrorlevel=0 

%myerrorlevel%...

0

Source: https://habr.com/ru/post/1759107/


All Articles