VOGONS


First post, by Cursed Derp

User metadata
Rank Oldbie
Rank
Oldbie

Hello,
I want to know is it possible to make a batch file in dos react in different ways when different crashes close q program that the batch file previously executed? When the batch file runs, I want to launch a program. When the program crashes and displays an error message in the dos terminal, I need the batch file to then launch different programs for different crashes. Crash type 1 loads program type 1, crash type 2 loads program type 2 and so on. Is it possible to implement this level of error handling in a dos batch file?
Thanks

I am as smooth as a gravy train with flaming biscuit wheels.

Reply 1 of 40, by leileilol

User metadata
Rank l33t++
Rank
l33t++

IF ERRORLEVEL

apsosig.png
long live PCem
FUCK "AI". It is a tool of fascism. We do not need it. We do not use it.

Reply 2 of 40, by jakethompson1

User metadata
Rank l33t
Rank
l33t

Yes, if errorlevel.
It is tricky. The if condition is considered "true" if the errorlevel is the number given or higher.
So if all you care about is detecting failure, you'd use if errorlevel 1. If you actually care about the errorlevel (e.g., you used the CHOICE command with three or more choices), you need to put the if errorlevel statements in descending order.

Reply 3 of 40, by Errius

User metadata
Rank l33t
Rank
l33t

this page is informative

https://www.robvanderwoude.com/errorlevel.php

"This all reminds me when i took the windows vista sticker thingy off my old laptop, and on my washing machine as a joke. A few days later said washing machine stopped working. I still think this cannot be a coincidence."

Reply 4 of 40, by DaveDDS

User metadata
Rank Oldbie
Rank
Oldbie

And or course it depends greatly if the application actually sets the errorlevel when it exits ....

I've run into a few that didn't, and I've had to write tools to scan the screen looking for messages.

But ... most things do set exit with meaningful exit codes , so hopefully it will be simple.

Btw ... I wrote a tool at one point to figure out where DOS actually stores exit codes (they are sometimes not easy to get directly)

- Dave ; https://dunfield.themindfactory.com ; "Daves Old Computers" ; SW dev addict best known:
ImageDisk: rd/wr ANY floppy PChardware can ; Micro-C: compiler for DOS+ManySmallCPU ; DDLINK: simple/small FileTrans(w/o netSW)via Lan/Lpt/Serial

Reply 5 of 40, by DaveDDS

User metadata
Rank Oldbie
Rank
Oldbie
jakethompson1 wrote on 2026-04-21, 16:26:

... It is tricky. The if condition is considered "true" if the errorlevel is the number given or higher. ...

Indeed, we had a discussion on this a while back and I wrote a script to show how it works:

Re: MS-DOS autostart CD without getting Abort-Retry-Fail

- Dave ; https://dunfield.themindfactory.com ; "Daves Old Computers" ; SW dev addict best known:
ImageDisk: rd/wr ANY floppy PChardware can ; Micro-C: compiler for DOS+ManySmallCPU ; DDLINK: simple/small FileTrans(w/o netSW)via Lan/Lpt/Serial

Reply 6 of 40, by Cursed Derp

User metadata
Rank Oldbie
Rank
Oldbie

Thank you all for your helpful replies. I hope Doom has usable exit codes because that's what I'm trying to launch a different program with when it crashes. I should have mentioned that earlier, I apologize. I don't have a lot of experience writing complex batch files. For an example, if Doom crashes and reports a "visplane overflow" error (no more visplanes), can I launch a different program than if it crashed with a "invalid sprite number" error? I would appreciate any tips as to whether this specific implementation is possible. I will look into the information and sources already provided.
Thanks

I am as smooth as a gravy train with flaming biscuit wheels.

Reply 7 of 40, by Cursed Derp

User metadata
Rank Oldbie
Rank
Oldbie

I intend for the game to be launched, then to intentionally crash and launch a seperate program. Different program depending on the type of crash. I also found this website about errorlevel: https://tutorialreference.com/batch-scripting … heck-exit-codes
I hope it's not written by ai, but it has some good information about the terminology for error code numbers. How would I know what code number is reported so I can add it in the batch file?

I am as smooth as a gravy train with flaming biscuit wheels.

Reply 8 of 40, by DaveDDS

User metadata
Rank Oldbie
Rank
Oldbie

ERRORLEVEL is actually based on a very simple concept that almost all operating systems support. When a program terminates, it can pass an "exit code" value back to whatever invoked it (with DOS usually the command procssor but it could be another program).

In DOS, that exit value is a byte which means it can be anywhere from 0 to 255. Most OSs agree that 0 means "success" and non-zero means "something went wrong" an this value might help any calling program understand what.

Low values are mostly defined in DOS - I don't recall the exact values now, but things like "bad argument(s)", "file not found", "write error" etc. but higher values are "up tp the application" and not standardized. Which means you have to know what a particular value means from a specific application.

The DOS command processor mostly ignores exit codes, but at least when processing "batch" files it "remembers" the exit code from the last program run, and let you test it with "IF ERRORLEVEL ..." ... sadly it doesn't officially make this available in any other way which means you have no way to save it (other than 255 test/sets) or recall the exit code from a previously run program (at one point I wrote a little tool "ERRLVL {options} command ..." which has options to show exit code, save it to a file, write it to a spot in memory (of the calling program) etc.)

But... since DOS never made much "fuss" about exit codes, many early programmers (and dev tools) just exited without setting it. Fortunately this got better over the years.

PS: You could use my ERRLVL tool to figure out exactly what exit code are returned by a program (ie: game) when ended in various ways... I'm pretty sure it's on my site somewhere, if that sounds useful I can figure out exactly where and let you know.

- Dave ; https://dunfield.themindfactory.com ; "Daves Old Computers" ; SW dev addict best known:
ImageDisk: rd/wr ANY floppy PChardware can ; Micro-C: compiler for DOS+ManySmallCPU ; DDLINK: simple/small FileTrans(w/o netSW)via Lan/Lpt/Serial

Reply 9 of 40, by jakethompson1

User metadata
Rank l33t
Rank
l33t

On the chance that you're using Windows 95/98/ME DOS ("DOS 7.0" or "DOS 7.1") it appears there is an undocumented /Z parameter to command.com to show the exit codes. i.e. you'd put in config.sys:
SHELL=C:\COMMAND.COM /P /Z

You may also come across echo %errorlevel%. That's on NT; I don't remember if it was also added to Windows 95 DOS or not. If so, it may only work from inside a batch file and not interactively.

Reply 10 of 40, by DaveDDS

User metadata
Rank Oldbie
Rank
Oldbie

I wasn't aware of /Z (thanks), I do know that %ERRORLEVEL% was added in later WinDOS ... but core DOS itself for the longest time (many revisions) did not provide a way to retrieve the actual exit code value sent back to COMMAND.COM - just test it above a set value within .BAT files.

I even went so far as to write a TSR which scanned all DOS memory, stuffing the keyboard buffer to run a tool that generated known specific exit codes in order to find where in memory DOS was "remembering" them ... That worked(ish) on my main system, but such undocumented information can't be trusted to use in anything you want to run anywhere else or give to others.

I do much of my DOS development these days in DosBox which I don't think offers either of these... I still use the "ERRLVL" tool when I need to have a valid method supported everywhere.

I still run a few actual DOS systems (mostly DOS 5- one PoQet running DOS 3) but other than one tiny laptop, don't use 32-bit Windows / "Windows on DOS" anywhere anymore (except for a couple early Win in VMs I used sometimes for testing)

- Dave ; https://dunfield.themindfactory.com ; "Daves Old Computers" ; SW dev addict best known:
ImageDisk: rd/wr ANY floppy PChardware can ; Micro-C: compiler for DOS+ManySmallCPU ; DDLINK: simple/small FileTrans(w/o netSW)via Lan/Lpt/Serial

Reply 11 of 40, by Errius

User metadata
Rank l33t
Rank
l33t

Is there any difference between ending a program with return 1 and exit(1)

"This all reminds me when i took the windows vista sticker thingy off my old laptop, and on my washing machine as a joke. A few days later said washing machine stopped working. I still think this cannot be a coincidence."

Reply 12 of 40, by jmarsh

User metadata
Rank Oldbie
Rank
Oldbie
Errius wrote on 2026-04-23, 09:20:

Is there any difference between ending a program with return 1 and exit(1)

Generally most compilers use program startup code that executes "exit(main())", so no.

Reply 13 of 40, by DaveDDS

User metadata
Rank Oldbie
Rank
Oldbie
Errius wrote on 2026-04-23, 09:20:

Is there any difference between ending a program with return 1 and exit(1)

Depends on the development tools used - since many programmers didn't bother to set the return code, some dev tools returned success unless you explicitly set an exit code.

Remember this was back in the day when C was simple K&R - there was no "void", functions return types defaulted in "int", many programmers would just either reach the end of "main" or just use "return;" to end the program. The library entry code only called "main" and didn't know if it actually returned a value or not - so if main returns, assume success (better than exiting with whateve random value happened to be in the accumulator). Take for example the classic K&R "Hello World" program which was often used as the simplest example of a "complete" C program - It didn't declate a funciton return type, nor did it "return" a value.

When I ported my compiler to PC (originally written before the PC was a thing), I chose this method - Micro-C is a K&R compiler, and if main just returns it exits to DOS with exit code=0 - to fail, you have to explicitly exit(code); - I actually prefer this approach, it males you think about passing back an exit code more... (and no need to go back a fix ancient code that didn't set exit) - to be safe/sure, I always exit(code) when I want to return an error condition no matter whos deve tools I'm using.

- Dave ; https://dunfield.themindfactory.com ; "Daves Old Computers" ; SW dev addict best known:
ImageDisk: rd/wr ANY floppy PChardware can ; Micro-C: compiler for DOS+ManySmallCPU ; DDLINK: simple/small FileTrans(w/o netSW)via Lan/Lpt/Serial

Reply 14 of 40, by Cursed Derp

User metadata
Rank Oldbie
Rank
Oldbie

So is there a way in dosbox to use a batch file to find the error level number of an error? Then I will be able to have the script react to different types of errors

I am as smooth as a gravy train with flaming biscuit wheels.

Reply 15 of 40, by DaveDDS

User metadata
Rank Oldbie
Rank
Oldbie

I don't know of a way in the DosBox that I use, perhaps newer releases have this ability. but I doubt it pre-Win DOS never had the ability to actually get a programs exit code within a batch file ... you could only test >n.

But I suspect what you want it to know what exit code is retrned on a certain error. You *could* do the annoying/painful way with a .BAT file:

program args
if errorlevel 1 echo 1
if errorlevel 2 echo 2
... repeat for everything between 3 and 253 ...
if errorlevel 254 echo 254
if errorlevel 255 echo 255

Then run the program, cause the error you want to test, and see what is the last number the batch file prints

Or, do what I do and use my little ERRLVL tool to run the program which will tell you it's exit code:

ErRoR LeVeL test

use: ERRLVL [options] value(0-255) [1]
"" "" program_file [arguments] [2]
opts: -Q Quiet (less output)
-Maddress write exit-code to Memory at address
-Bfilename "" to Binary file \ Mutually
-T"" "" Text file > exclusive
-Ccommand execute Command (with exit-code) / (only one)
[1] Exits with the specified exit-code value (ERRORLEVEL)
[2] runs a program and reports it's exit-code
may optionally write that exit-code to: Memory or File
or execute a command with it in args.
if program_file does not exist in current directory, ERRLVL will search along
your PATH.
If program_file has no extension, ERRLVL will try: .BAT .COM and .EXE

address assumes hex \_ available : %=binary $=hex
value assumes decimal/~ base prefix: @=octal .=Decimal
-Ccommand may: '~_'=' ' '~('='<' '~~'='~' '~%@$.'=exit-code
contain : '~!'='|' '~)'='>' (%binary/@octal/$hex/.decimal)

Dave Dunfield - https://dunfield.themindfactory.com

Note, without -Q ERRLVL will show the programs exit code, no need to do anything special.

- Dave ; https://dunfield.themindfactory.com ; "Daves Old Computers" ; SW dev addict best known:
ImageDisk: rd/wr ANY floppy PChardware can ; Micro-C: compiler for DOS+ManySmallCPU ; DDLINK: simple/small FileTrans(w/o netSW)via Lan/Lpt/Serial

Reply 16 of 40, by Cursed Derp

User metadata
Rank Oldbie
Rank
Oldbie
DaveDDS wrote on 2026-04-23, 14:56:
I don't know of a way in the DosBox that I use, perhaps newer releases have this ability. but I doubt it pre-Win DOS never had t […]
Show full quote

I don't know of a way in the DosBox that I use, perhaps newer releases have this ability. but I doubt it pre-Win DOS never had the ability to actually get a programs exit code within a batch file ... you could only test >n.

But I suspect what you want it to know what exit code is retrned on a certain error. You *could* do the annoying/painful way with a .BAT file:

program args
if errorlevel 1 echo 1
if errorlevel 2 echo 2
... repeat for everything between 3 and 253 ...
if errorlevel 254 echo 254
if errorlevel 255 echo 255

Then run the program, cause the error you want to test, and see what is the last number the batch file prints

Or, do what I do and use my little ERRLVL tool to run the program which will tell you it's exit code:

ErRoR LeVeL test

use: ERRLVL [options] value(0-255) [1]
"" "" program_file [arguments] [2]
opts: -Q Quiet (less output)
-Maddress write exit-code to Memory at address
-Bfilename "" to Binary file \ Mutually
-T"" "" Text file > exclusive
-Ccommand execute Command (with exit-code) / (only one)
[1] Exits with the specified exit-code value (ERRORLEVEL)
[2] runs a program and reports it's exit-code
may optionally write that exit-code to: Memory or File
or execute a command with it in args.
if program_file does not exist in current directory, ERRLVL will search along
your PATH.
If program_file has no extension, ERRLVL will try: .BAT .COM and .EXE

address assumes hex \_ available : %=binary $=hex
value assumes decimal/~ base prefix: @=octal .=Decimal
-Ccommand may: '~_'=' ' '~('='<' '~~'='~' '~%@$.'=exit-code
contain : '~!'='|' '~)'='>' (%binary/@octal/$hex/.decimal)

Dave Dunfield - https://dunfield.themindfactory.com

Note, without -Q ERRLVL will show the programs exit code, no need to do anything special.

Whoa thank you! I will use this script to try to get the error code. Sounds much better than doing it the long way

I am as smooth as a gravy train with flaming biscuit wheels.

Reply 17 of 40, by jakethompson1

User metadata
Rank l33t
Rank
l33t
DaveDDS wrote on 2026-04-23, 14:56:
Then run the program, cause the error you want to test, and see what is the last number the batch file prints […]
Show full quote
program args
if errorlevel 1 echo 1
if errorlevel 2 echo 2
... repeat for everything between 3 and 253 ...
if errorlevel 254 echo 254
if errorlevel 255 echo 255

Then run the program, cause the error you want to test, and see what is the last number the batch file prints

That won't work because the if errorlevel tests need to be in descending order, because "if errorlevel 1" is true for anything other than zero. Edit: oh, you account for that since they only pay attention to the last line printed.
But I think OP is going to just use your ERRLVL tool anyway, which will be easier.

Reply 18 of 40, by igully

User metadata
Rank Member
Rank
Member

To put out a specific errorlevel in DOS I have created a tiny tool called SEL (Set Errror Level). You just enter SEL 47 (or whatever number between 0 and 255) and you will set the errorlevel to that value. You can use it from the command line or from a batch file.

On the other hand, I found a Public Domain batch file which I have modified to suit this need. It is called "ERRLEVEL". It is also very small. When ran, it retrieves the errorlevel number of the last program ran (again, errorlevels will be displayed as numbers between 001 and 255) and simply displays it on the command line.

Just try them, both are convenient and small tools to integrate within your batch files are are Public Domain software.

Reply 19 of 40, by DaveDDS

User metadata
Rank Oldbie
Rank
Oldbie
jakethompson1 wrote on 2026-04-23, 21:14:

... That won't work because the if errorlevel tests need to be in descending order ...

Yes it will work exactly as I intended.

You are correct that all values >= the actual errorlevel will be "echo"d, but unlike most processing of ERRORLEVEL in batch files it in not performing a GOTO, which means all erroelevel tests will be performed.

If you are lucky enough to have a 256 line screen, you will see all possible outputs.

But ... if you have a 25 line DOS screen (like me), you will only see about the last 23 outputs - had I placed the statements in the normal "descending" order, for any exit code high enough, you would only see 24-1 ... the way I did it the last line output will be the actual errorlevel, and"echo"s for higher values will not be output.

(for low errorlevels, you will get 255-n which could be a LOT of lines, but the one you are interested in will be the last one displayed)

(I did explain how errorlevel works in the thread I linked)

But ... unless you want to generate such a batch file, who wants to type in all those lines ... much easier to use my ERRLVL tool!

Btw, I checked and I hadn't actually put ERRLVL in my downloads, so I added it to the "DBUTILS" archive!

Last edited by DaveDDS on 2026-04-24, 11:30. Edited 1 time in total.

- Dave ; https://dunfield.themindfactory.com ; "Daves Old Computers" ; SW dev addict best known:
ImageDisk: rd/wr ANY floppy PChardware can ; Micro-C: compiler for DOS+ManySmallCPU ; DDLINK: simple/small FileTrans(w/o netSW)via Lan/Lpt/Serial