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:
- QuickBasic versions 1.00, 1.01, 1.02, 2.00, 2.01, 3.00, 4.00, 4.00b, and 4.50 for MS-DOS
- Microsoft GW-Basic versions 3.20, 3.22, and 3.23 for MS-DOS
- 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
Basic 7.00 or 7.10 Can Return Exit Code (ERRORLEVEL) to Batch FileAn .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:
PRINT "This is a Basic program that returns an exit code of 5."
n% = 5
IF NOT ERRORLEVEL 1 GOTO DONE
ECHO An error occurred with exit code 1 or higher.
ECHO End of batch file.
Using a File as a Flag for a Batch FileThe 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:
if not exist errfil goto end
echo An error occurred during program running
echo End of batch file
' set up to error out if "GARBAGE.DAT" does not exist
ON ERROR GOTO errorlevel
OPEN "garbage.dat" FOR INPUT AS #1
OPEN "errfil" FOR OUTPUT AS #1 'Create file that acts as a flag
SYSTEM ' Returns to DOS.
Article ID: 41533 - Last Review: 21/11/2006 - Revision: 1