Windows batch provides several forms of the IF statement:
-
IF <condition> <command> -
IF EXIST <filename> <command> -
IF DEFINED <variable> <command> -
IF ERRORLEVEL <number> <command> -
IF <string1>==<string2> <command>IF <condition> (commands) ELSE (commands)
Or, without parentheses:
IF <condition> command1 ELSE command2
However, parentheses are preferred for multi-line blocks and to avoid ambiguity.
-
IF "string1"=="string2" (echo match) ELSE (echo no match)
Always quote your strings to avoid parsing issues, especially when dealing with spaces or empty variables.
-
:: Problematic when variable is empty! IF %USERNAME%==admin echo Welcome :: Better: IF "%USERNAME%"=="admin" echo Welcome
If
%USERNAME%is empty, the first line becomes:IF ==admin echo Welcome
...which causes a syntax error.
Batch string comparison using
==is case-insensitive by default.IF "admin"=="ADMIN" echo This will match
To do case-sensitive comparisons, use
CALLwith string manipulation or external tools (not natively supported).
Consider this:
SET VAR=hello IF "%VAR%"=="hello" ECHO match
Now try:
SET VAR=hello & SET VAR=world IF "%VAR%"=="world" ECHO match
This won’t work reliably in complex loops. You’ll need delayed variable expansion:
SETLOCAL ENABLEDELAYEDEXPANSION SET VAR=hello IF "!VAR!"=="hello" ECHO match ENDLOCAL
Use
!VAR!instead of%VAR%when using delayed expansion, especially inside loops orIFblocks.
-
IF EXIST "filename.txt" ( ECHO File found! ) ELSE ( ECHO File not found! )
-
IF EXIST "C:\MyFolder\" ECHO Folder exists
Yes, folders are treated like files with trailing slashes.
Avoid combining wildcards and quotes:
:: WRONG – This won't work IF EXIST "*.txt" echo Match found :: Correct IF EXIST *.txt echo Match found
Quotes are fine for exact names, not wildcard patterns.
IF EXIST "file.txt" ( IF "%MODE%"=="debug" ( ECHO Found file in debug mode ) ELSE ( ECHO Found file in normal mode ) ) ELSE ( ECHO File not found )
Just like other languages, you can nest conditionals, but be mindful of readability.
-
All
IFstring comparisons using==are string-based, not numeric.SET A=10 SET B=2 IF %A% GTR %B% echo A is greater
-
Use these:
EQU– EqualNEQ– Not equalLSS– Less thanLEQ– Less than or equalGTR– Greater thanGEQ– Greater than or equal
Example:
SET /A A=5 IF %A% GEQ 3 echo A is 3 or more
🔍
%A% GEQ 3works only if both sides are numeric.
-
Checks the exit code from the previous command:
ping -n 1 127.0.0.1 IF ERRORLEVEL 1 ( ECHO Ping failed ) ELSE ( ECHO Ping succeeded )
Important:
IF ERRORLEVEL 2 … :: Matches 2, 3, 4, … (NOT just 2)
-
To check for exact error level, check from highest to lowest:
IF ERRORLEVEL 3 ( ECHO 3 or more ) ELSE IF ERRORLEVEL 2 ( ECHO 2 ) ELSE IF ERRORLEVEL 1 ( ECHO 1 )
-
Checks if a variable exists:
IF DEFINED PATH ( ECHO PATH is set ) ELSE ( ECHO PATH is not set )
Great for checking if user or system variables are available.
| Mistake | Why it’s a problem |
|---|---|
| Unquoted strings | Breaks when variables are empty or contain spaces |
| No delayed expansion in loops | %VAR% gets stale values |
| Wrong file path in IF EXIST | Missing quotes, wrong slashes, case sensitivity |
| Assuming ERRORLEVEL means exact | It's ≥ comparison, not equality |
@ECHO OFF
SET FILE=backup.zip
IF EXIST "%FILE%" (
ECHO Found existing backup. Deleting...
DEL "%FILE%"
)
ECHO Creating new backup...
REM simulate backup with dummy file
COPY NUL "%FILE%"
IF EXIST "%FILE%" (
ECHO Backup created successfully.
) ELSE (
ECHO Backup failed!
)IF EXIST "input.txt" IF "%MODE%"=="prod" (
ECHO Processing file in prod mode
)Or use nested form for clarity.
| Type | Example | Description |
|---|---|---|
| String Compare | IF "%A%"=="B" |
String equality |
| File Check | IF EXIST "file.txt" |
File or folder presence |
| Variable Check | IF DEFINED MYVAR |
Whether a variable is defined |
| Numeric Compare | IF %X% GTR 3 |
Numerical comparisons |
| Exit Code | IF ERRORLEVEL 1 |
Previous command's exit status |