batch-fileIf statements


Syntax

  • if [/i] StringToCompare1 == StringToCompare2 (commandA) else (commandB)

  • if errorlevel 1 (commandA) else (commandB)

  • if %errorlevel% == 1 (commandA) else (commandB)

  • if exist Filename (commandA) else (commandB)

  • if defined VariableName (commandA) else (commandB)

Remarks

There are a few syntax to choose from in an if statement. We will use if string1==string2 as an example.

1-Line Syntaxes

  • if string1==string2 commandA
  • if string1==string2 (commandA)
  • if string1==string2 (commandA) else (commandB)
  • if string1==string2 (commandA) else commandB
  • if string1==string2 (commandA)else (commandB)
  • if string1==string2 (commandA)else commandB

Multiline Syntaxes

if string1==string2 (
    commandA
)

Or

if string1==string2 (
    commandA
) else (
    commandB
)

There are still some extra syntaxes available.