Microsoft KB Archive/41533

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 17:56, 18 July 2020 by 3155ffGd (talk | contribs) (importing KB archive)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Article ID: 41533

Article Last Modified on 11/21/2006

This article was previously published under Q41533

SUMMARY

MS-DOS batch processing (.BAT) files can use an "IF ERRORLEVEL n" statement to detect exit code levels returned by some programs.

However, the only versions of Microsoft Basic that allow a program to return an error level code to MS-DOS are Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10. The END n or STOP n statement returns error level n to the batch file that invoked the Basic 7.00 or 7.10 .EXE program. The IF ERRORLEVEL n statement in the batch file can detect if the returned exit code is equal to or greater than n.

In all other versions of Microsoft Basic, the error level (exit) code returned by a Basic program is controlled by the Basic run-time module, not by your program. As an alternative, you can create a file in the Basic program to serve as a flag when a certain condition occurs. The batch file that called your program can then check for the existence of the flag file in place of checking for an error level. In batch files, the "IF EXIST filename" command can be used.

The following products do not allow your program to return an error level to MS-DOS batch files:

  1. QuickBasic versions 1.00, 1.01, 1.02, 2.00, 2.01, 3.00, 4.00, 4.00b, and 4.50 for MS-DOS
  2. Microsoft GW-Basic versions 3.20, 3.22, and 3.23 for MS-DOS
  3. Microsoft Basic Compiler versions 5.35 and 5.36 for MS-DOS and versions 6.00 and 6.00b for MS-DOS and MS OS/2


MORE INFORMATION

Your Basic program must not attempt to invoke any MS-DOS interrupts (CALL INTERRUPT) to terminate the program with an error level; otherwise, strange results may occur and the machine may hang. Basic must handle program termination by itself.

Basic 7.00 or 7.10 Can Return Exit Code (ERRORLEVEL) to Batch File

An .EXE program compiled in Basic 7.00 or 7.10 can use the STOP n% or END n% statement to return an exit code (n%) to MS-DOS, as follows:

   ' TEST.BAS
   PRINT "This is a Basic program that returns an exit code of 5."
   n% = 5
   END n%
                

The exit code can be trapped in a MS-DOS batch file with the IF ERRORLEVEL n GOTO statement, as follows:

   TEST
   ECHO OFF
   IF NOT ERRORLEVEL 1 GOTO DONE
      ECHO  An error occurred with exit code 1 or higher.
   :DONE
   ECHO End of batch file.
                

Using a File as a Flag for a Batch File

The following technique lets any Basic version give a simple yes or no message to a batch file.

The following batch file, ERRT.BAT, calls the Basic program ERRTST, which drops back to the batch file. It then checks for the existence of the file ERRFIL (which is an arbitrary name) to see if an error occurred while running the Basic program:

   echo off
   del errfil
   errtst
   if not exist errfil goto end
   echo An error occurred during program running
   :end
   echo End of batch file
                

The following file is ERRTST.BAS; it creates the error file if it cannot open the file GARBAGE.DAT:

' set up to error out if "GARBAGE.DAT" does not exist
ON ERROR GOTO errorlevel
OPEN "garbage.dat" FOR INPUT AS #1
CLOSE #1
END
errorlevel:
   CLOSE #1
   OPEN "errfil" FOR OUTPUT AS #1   'Create file that acts as a flag
   CLOSE #1
   SYSTEM   ' Returns to DOS.
                

To demonstrate this procedure, compile and link ERRTST.BAS as follows:

   BC ERRTST.BAS;
   LINK ERRTST.OBJ;
                

Now run the batch file ERRT.BAT. If the Basic program cannot find GARBAGE.DAT, ERRT.BAT shows "An error occurred during program running."


Additional query words: QuickBas BasicCom

Keywords: KB41533