SHORTIES\ERLVLTST.DOC  ·  DOC  ·  2.2 KB  ·  1989-09-19  ·  from PCPlus_Issue-41_Feb-1990_FluxEngine-360Kb

   
   
   ERLVLTST.BAT  -  Convert DOS ErrorLevel to an environment parameter
   ~~~~~~~~~~~~
   
   Some of  the DOS  commands and many utilities (such as some batch file menu
   utilities), return an ErrorLevel, sometimes called Exit Code.
   
   ErrorLevel can be tested by the DOS command:
   
       IF ERRORLEVEL (n) (Instruction) (Parameter)
   
   The above test has four drawbacks:
   1.  ErrorLevel is not a parameter, you cannot display it, e.g. do:
       ECHO ERRORLEVEL.
   2.  The "IF"  test is  not for  equality, but for a value in excess of (n),
       i.e. you  must be  careful to test in a descending order if the test is
       to be used for branching (GOTO).
   3.  You cannot  guarantee that  ErrorLevel will  be available  when wanted,
       because another command might have modified it.
   4.  Most DOS  manuals do  not specify  ErrorLevels of  DOS commands that do
       return them.
   
   ERLVLTST.BAT solves  the above  problems by  making ErrorLevel available to
   the user  as an environment parameter, called %ERRVAL%.  This parameter can
   have a range of 0 to 255.
   
   The most  likely use  for this utility is during development of batch files
   that use  GOTOs controlled  by ErrorLevel.  After  having issued  a command
   that returns an errorlevel (e.g. FORMAT, XCOPY, SUBST, etc.), type
       ERLVLTST /D.
   The value of ErrorLevel will then be displayed and stored.
   
   If you  use DOS  3.30 or  above, this  batch file  can be  imported  as  an
   external utility by inserting it as:
       CALL ERLVLTST /N
   or  CALL ERLVLTST /D
   
   Finally, it may be interesting to view the technique employed here  and use
   it in other batch files.  Compare the following:
   
   The conventional way to do branch testing is:
       IF ERRORLEVEL 10 GOTO LABEL10
       IF ERRORLEVEL  9 GOTO LABEL9
       IF ERRORLEVEL  8 GOTO LABEL8
            and so on ...
            etc.
       IF ERRORLEVEL  0 GOTO LABEL0
   
   Using the above technique is tedious, it can be reduced to:
       FOR %%p IN (0 1 2 3 4 5 6 7 8 9 10) DO IF ERRORLEVEL %%p SET ERRVAL=%%P
       GOTO LABEL%ERRVAL%
   
   And that is all there is to it.
   
   
   M. Greenfield    19 September, 1989