if-statementbatch-filecmdbatch-choice

I can't understand choice error codes of batch


If I have a code like this:

@echo off
choice /C BL /M "Clear screen or list actual directory"

if errorlevel 2 goto l
if errorlevel 1 goto b

:l
tree /f
goto final

:b
cls
goto final

:final

I know this actually works, but I'm confused about one thing about the errorlevel parts. I wrote first, the same code, but like this:

if errorlevel 1 goto l
if errorlevel 2 goto b

And that way it won't work properly It will only remember the errorcode 1. If you press the second option is not working.
I'm really wondering why the order of the errors matters, if a batch its supposed to execute line by line, or am i wrong?
In a nutshell, what I want to understand is how the error codes work here


Solution

  • C:\>if /?
    ...
    IF [NOT] ERRORLEVEL number command
    ...
    ERRORLEVEL number   Specifies a true condition if the last program run returned
                        an exit code equal to or greater than the number specified.

    In other words, if errorlevel 1 executes for any errorlevel (except 0 = no error) because they're all equal to or greater than 1.