batch-file If statements

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any batch-file Question?