Search notes:

cmd.exe: if

if can be used to conditionally execute parts in a batch file.

Simplest form

In its simplest form, the if keyword is followed by a comparison (or condition) and a command. The command is executed if the comparision is true.
In the following example, the if statement is prefixed with an at sign (@) in order to prevent the batch file interpreter from echoing the entire if statement when executed.
@if %1 == foo echo first argument is foo
Github repository about-cmd.exe, path: /commands/if/if_.bat

if … else

An else part can be added by
@if %1 == foo ( echo first argument is foo ) else echo first argument is NOT foo
Github repository about-cmd.exe, path: /commands/if/if_else.bat

Using parentheses

Using parentheses also allow to add more than one command to be executed to the true or false part of an if statement. I generally find they make it easier to read if statements:
@echo off

if "%1" equ "foo" (
  
   echo This is the foo
   echo ---------------
   echo Paranthteses allow to add
   echo more than one comamd

) else (

   echo This is NOT the foo
   echo -------------------

)
Github repository about-cmd.exe, path: /commands/if/else.bat

if … else if … else

Multiple ifelse ifelse statements can be chained:
@echo off

if %1 == foo (

   echo the first argument
   echo is foo

) else if %1 == bar (

   echo the first argument
   echo is bar

) else if %1 == baz (

   echo the first argument
   echo is baz

) else (

   echo The first argument
   echo is neither foo nor
   echo bar nor baz.

)
Github repository about-cmd.exe, path: /commands/if/else-if.bat

Verifiying that an argument was passed to a batch file

The previous examples assumed that the batch files were invoked with at least one argument. It generally is good practice to verify such an assumption in a batch file:
@echo off

if [%1] neq [] goto ok

echo you need to pass an argument

exit /b

:ok

@echo You have specified %1
Github repository about-cmd.exe, path: /commands/if/neq_check-empty-variable.bat
Such a verification can also be achieved with if defined:
@echo off
set arg=%1

if defined arg (

   echo You have specified %arg%

) else (

   echo You need to specify an argument to the script

)
Github repository about-cmd.exe, path: /commands/if/neq_check-empty-variable_2.bat
See also check if a variable is defined

Check if a file exists

if followed by the keyword exist (not exists) checks if a file exists. if exist can also be used on wildcards (such as *.txt):
@echo off

if exist exist.bat (
   echo The file exists
) else (
   echo The file does not exists
)

if exist foo.bar (
   echo The file exists
) else (
   echo The file does not exists
)

if exist *.bat (
   echo There are .bat files in this directory
) else (
   echo No .bat files in this directory
)

if exist *.txt (
   echo There are .txt files in this directory
) else (
   echo No .txt files in this directory
)
Github repository about-cmd.exe, path: /commands/if/exist.bat
It is also possible to check if a file does not exist with if not exist….

Comparison operators

There are seven comparison operators:
equ equal
neq not equal
lss less than
leq less than or equal
gtr greater than
geq greater than or equal
== equal (always compares alphabetically?)
If both compared values are integers (positive or negative), they're compared numerically, otherwise, they're comparred alphabetically. This is demonstrated with the following batch file:
@echo off

@set num_a=99
@set num_b=100
@set num_c=100.1
@set num_d=-30
@set num_e=-50

@set txt_a="99"
@set txt_b="100"
@set txt_c=foo
@set txt_d=bar

if %num_a%    EQU   %num_b% ( echo %num_a%  = %num_b% ) else ( echo %num_a%  != %num_b%  )
if %num_a%    NEQ   %num_b% ( echo %num_a% != %num_b% ) else ( echo %num_a%   = %num_b%  )
if %num_a%    LSS   %num_b% ( echo %num_a% ^< %num_b% ) else ( echo %num_a% ^>= %num_b%  )
if %num_a%    GTR   %num_b% ( echo %num_a% ^> %num_b% ) else ( echo %num_a% ^<= %num_b%  )
if %num_a%    GTR   %num_c% ( echo %num_a% ^> %num_c% ) else ( echo %num_a% ^<= %num_c%  )
if %num_d%    GTR   %num_e% ( echo %num_d% ^> %num_e% ) else ( echo %num_d% ^<= %num_e%  )

if %txt_a%    EQU   %txt_b% ( echo %txt_a%  = %txt_b% ) else ( echo %txt_a%  != %txt_b%  )
if %txt_a%    NEQ   %txt_b% ( echo %txt_a% != %txt_b% ) else ( echo %txt_a%   = %txt_b%  )
if %txt_a%    LSS   %txt_b% ( echo %txt_a% ^< %txt_b% ) else ( echo %txt_a% ^>= %txt_b%  )
if %txt_c%    LSS   %txt_d% ( echo %txt_c% ^< %txt_d% ) else ( echo %txt_c% ^>= %txt_d%  )
Github repository about-cmd.exe, path: /commands/if/comparison-operators.bat
The batch file prints
99  != 100
99 != 100
99 < 100
99 <= 100
99 > 100.1
-30 > -50
"99"  != "100"
"99" != "100"
"99" >= "100"
foo >= bar

Case insensitive comparison

if /i … compares strnigs case insensitively:
@echo off

set txt_a=foo
set txt_b=FOO

if    %txt_a% equ %txt_b% ( echo %txt_a% = %txt_b% ) else ( echo %txt_a% != %txt_b% )
if /i %txt_a% equ %txt_b% ( echo %txt_a% = %txt_b% ) else ( echo %txt_a% != %txt_b% )
Github repository about-cmd.exe, path: /commands/if/case-insensitive.bat
The script prints
foo != FOO
foo = FOO

See also

if is affected by whether command extensions are enabled or disabled.
Iterating over a series of numbers
if being used in choice.exe.

Index