Command: if
IF performs conditional processing in batch programs.
IF is a BATCH-FILE / AUTOEXEC.BAT command.
It can also be used in command line.
Syntax:
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
drive The drive letter, e.g. C:
path The directory, e.g. \example\
filename The file, e.g. test.txt
Options:
NOT Specifies that the command shell should carry out
the command only if the condition is false.
(Without this, the command will be run if the
condition is true.)
ERRORLEVEL number Specifies a true condition if the last program run
returned an exit code equal to or greater than the
number specified.
command Specifies the command to carry out if the condition
is met.
string1==string2 Specifies a true condition if the specified text
strings match.
EXIST filename Specifies a true condition if the specified filename
exists.
Comments:
ERRORLEVEL number: DOS Programs return a number when they exit,
which sometimes contains information on
whether the program was successful. If the
last program to exit returned the given number,
then the condition is true.
string1==string2 If the two strings of characters are equal,
then the condition is true.
exist [drive][path] If the given file is there, then the condition
filename is true.
IF is a command internal to command.com and needs no other file
in order to work.
Examples:
IN A .BAT FILE / AUTOEXEC.BAT:
if exist c:\kernel.sys ECHO Kernel exists
(gives a message if kernel exists)
set A=freedos
set B=freedos
set C=otherdos
if %A%==%B% ECHO A and B are same string
(A and B are "freedos", so the message will appear)
if %A%==%C% ECHO A and C are same string
(A is "freedos", C is "otherdos", so no message
will appear)
if not %A%==%C% ECHO A and C are not same string
(A is "freedos", C is "otherdos", as they are NOT
the same, the message will appear)
See also:
autoexec.bat
batch files
choice
goto
------------------------------------------------------------------------------
Copyright (C) 2003 Robert Platt, updated 2007 and 2020 by W. Spiegl.
This file is derived from the FreeDOS Spec Command HOWTO.
See the file H2Cpying for copying conditions.