Search code examples
batch-filedos

Issues with IF EXIST ELSE being ignored in DOS 6.22 Batch file


I'm currently working on an old legacy MS-DOS 6.22 system that now requires different batch files to be called depending on if a file exists on C:.
I placed the following in AUTOEXEC.BAT:

IF EXIST C:\IMGOK.TXT (BOOT.BAT) ELSE (LOADIMG.BAT)

Something must be off because it seems to be ignoring this statement and calling neither of the .BAT files I specified. This is regardless of if IMGOK.TXT exists. IMGOK.TXT is an empty text file created via
COPY NUL>IMGOK.TXT
I have tried both with and without the parenthesis around the statements.

Am I missing something with how to use this IF EXIST ELSE statement? Thank you in advance for the help.

EDIT: Got it, can't use ELSE in DOS batch files. Had to use IF EXISTS and IF NOT EXISTS for my different calls. Thanks!


Solution

  • MS-DOS doesn't support the ELSE keyword. It also doesn't support using parentheses to group commands. This means if the file C:\IMGOK.TXT exists then the command (BOOT.BAT is executed with arguments ELSE (LOADIMG.BAT). Since you probably don't have a file named (BOOT.BAT this will cause an error if C:\IMGOK.TXT exists and do nothing if it doesn't.

    A simple way to solve your problem is to use two IF statements:

     IF EXIST C:\IMGOK.TXT BOOT.BAT
     IF NOT EXIST C:\IMGOK.TXT LOADIMG.BAT
    

    Since running BOOT.BAT will end further processing of commands in AUTOEXEC.BAT you can also do:

     IF EXIST C:\IMGOK.TXT BOOT.BAT
     LOADIMG.BAT