Conditional Startup Logic in MS-DOS AUTOEXEC.BAT Using IF Statements
Learn how to use IF EXIST and IF ERRORLEVEL in MS-DOS batch to conditionally load network drivers in AUTOEXEC.BAT, with examples, limitations, and verification steps.
11 Dec 2025, 21:42 UTC

Problem: Choosing startup actions based on hardware
When booting an MS-DOS machine, you often want to run a network stack only if a network card driver is present, and otherwise show a message or run a different utility. The built-in COMMAND.COM batch language offers only a few flow-control primitives, so you need to decide whether the IF statement can reliably branch execution in AUTOEXEC.BAT.
How IF works in COMMAND.COM
COMMAND.COM provides two basic IF forms that are useful for startup decisions:
IF EXIST path command– true if the file or directory exists.IF ERRORLEVEL n command– true if the most recent program returned an exit code >= n.
Both forms support an optional NOT prefix and an optional ELSE clause, but there is no true block structure; each IF is a single line.
Worked example: Conditional network startup
@ECHO OFF
IF EXIST C:\\NET\\NET.COM GOTO HASNET
ECHO No network driver detected.
GOTO END
:HASNET
C:\\NET\\NET.COM
IF ERRORLEVEL 1 GOTO DRVFAIL
ECHO Network driver loaded.
NET START
GOTO END
:DRVFAIL
ECHO Driver failed to load.
:END
This script first tests for the driver file. If present, it jumps to the HASNET label, runs the driver, checks ERRORLEVEL to see if it succeeded, and either starts the network or reports a failure. If the file is missing, it prints a message and skips the network steps.
Trade-offs and limitations
- No grouping: Complex conditions (e.g., “file exists AND error level = 0”) must be split into sequential checks, which can make the script harder to read.
- ERRORLEVEL semantics:
IF ERRORLEVEL 1triggers for any exit code ≥ 1, so you must order checks from highest to lowest when testing specific codes. - Case‑insensitive matching: File name checks ignore case, which is usually fine but can hide typos on case‑sensitive emulators.
- No ELSE block for nested logic: Each IF can have only a single trailing command or a GOTO; elaborate nesting requires additional labels.
Verification steps
- Create a floppy image or DOSBox folder containing
AUTOEXEC.BATwith the script above. - Place a dummy
NET.COMinC:\\NET\\to simulate a driver, or omit it to test the missing‑driver path. - Boot the MS-DOS environment.
- Observe the console output: you should see either “No network driver detected.” or the driver load messages followed by “Network driver loaded.” and the
NET STARTline. - To test the error path, replace
NET.COMwith a program that returns exit code 2 (e.g., a simpleDEBUGscript thatQwith2) and confirm the “Driver failed to load.” message appears.
If the output matches the expected branch, the IF statements are working as intended.
Actionable closing
For most startup-time hardware detection tasks in MS-DOS, the built-in IF EXIST and IF ERRORLEVEL checks provide a lightweight, reliable way to branch execution without external utilities. Keep the checks simple, order ERRORLEVEL tests carefully, and use labels or GOTO to simulate block structure. Test with a real boot or DOSBox to confirm the correct path runs, and you’ll have a robust AUTOEXEC.BAT that adapts to the hardware present at power-on.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.