πŸ“œ ⬆️ ⬇️

ERRORLEVEL is not% ERRORLEVEL%

The cmd.exe command processor has such a thing - an error level (error level). This is the exit code of the program that you ran last. You can check the error level using the IF ERRORLEVEL command.

IF ERRORLEVEL 1 ECHO error level is 1 or more

<sidebar>
Check IF ERROR LEVEL n works if the error level is n or higher . This is probably because many programs express varying degrees of error with large and large exit codes. For example, the diff program has 3 exit codes: β€œ0” means that the files are the same, β€œ1” is different, β€œ2” - something terrible happened. Some programs use exit code β€œ0” for success and everything else for error.
</sidebar>

In addition to this internal state, you can create an environment variable called ERRORLEVEL , if you wish, just as you can create a variable called FRED . But, like FRED , this variable will not affect the level of the error.

rem this next command sets the error level to zero
CMD /C EXIT 0
set ERRORLEVEL=1
if ERRORLEVEL 1 echo Does this print?


The message will not be displayed as the ERRORLEVEL variable has no effect on the error level. It is simply a variable whose name matches the concept of the command processor.
')
set BANKBALANCE=$1 000 000,00

β€œHey, when I try to withdraw money, I have a mistake -” not enough money in the account β€œ.”

However, there is an option when the extended command mode is enabled, and you use %ERRORLEVEL% .
In this case, the command processor looks for a variable with the same name and, if it does not find it, replaces %ERRORLEVEL% with the current value of the internal error level. This is a backup option - how to specify a neighbor's address as a spare shipping address, in case you are not at home. However, this will not affect the parcels delivered to the neighbor.

The same behavior and %CD% : if you have not set a variable with the same name, the current directory of the command processor is substituted. But you cannot change the directory with set CD=C:\Windows .

There are probably several reasons for this behavior:
- So that you can display the level of error in the log:
ECHO error level is %ERRORLEVEL%>logfile
- To allow other comparisons to be made with an error level β€” for example, to check for equality:
IF %ERRORLEVEL% EQU 1 echo Different!

But I deviated from the topic. Today, my thesis is this: the error level is not the same as the% ERRORLEVEL% variable.

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


All Articles