| 1. |
Solve : Having issues with "CHOICE" command?!? |
|
Answer» Hi guys, if ERRORLEVEL == 1 goto Start There are 2 alternative errorlevel formats you can use: (a) Old MS_DOS IF ERRORLEVEL syntax, levels in descending order if ERRORLEVEL 2 goto Start if ERRORLEVEL 1 goto Options (b) Later NT command language format, levels in any order if %ERRORLEVEL%==2 goto Start if %ERRORLEVEL%==1 goto Options Salmon Trout is right. A descending order needed. Says descending order right in the help file. Quote from: Squashman on July 13, 2015, 09:31:47 PM Says descending order right in the help file.Thank you.Some clarification... this must be about the 10th time I have written this... The old (true) MS-DOS legacy way of checking for errorlevels was LIKE this If errorlevel X do_command This meant "if the errorlevel is equal to or greater than X, do_command". Do_command was USUALLY goto a label. This meant that if you wanted to check for more than one expected errorlevel you had to do stuff like this some_command if errorlevel 5 goto egg if errorlevel 4 goto bacon if errorlevel 3 goto cheese echo Errorlevel was 0, 1 or 2! goto end :egg echo Errorlevel was 5 or more! goto end :bacon echo Errorlevel was exactly 4! goto end :cheese echo Errorlevel was exactly 3! :end echo Finished... The errorlevel in MS-DOS and Windows before NT was held in a byte and hence could be from 0 to 255. In true MS-DOS, if you wanted to actually find out what errorlevel a program or command returned, you had to do something like this... some_command if errorlevel 255 echo 255 if errorlevel 254 echo 254 if errorlevel 253 echo 253 ... (snipped 251 lines) if errorlevel 1 echo 1 if errorlevel 0 echo 0 256 lines just to get the errorlevel. All this was very cumbersome, and in Windows NT a pseudo environment variable %errorlevel% was added, which you could test in one line e.g.: echo %errorlevel% Also you can use all the compare-ops like EQU NEQ LSS LEQ GTR GEQ. Probably as well to remember that NT errorlevels are held in a 32 signed integer and can be anywhere from -2147483648 to +2147483647 (that's right, you can have errorlevels below zero as well as above, so the old IF ERRORLEVEL might not always do what you expect.) Further reading: http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx http://steve-jansen.github.io/guides/windows-batch-scripting/part-3-return-codes.html (I like Steve Jansen's tip about using powers of 2 for your own return codes, so you can use bitwise OR and test for multiple errors at once.) http://www.robvanderwoude.com/errorlevel.php |
|