if - Conditional Branching

Maintained on

When writing batch files, situations where you need to “branch processing based on a condition” come up very frequently.

For example: run a copy only when a file exists, or proceed to the next step only if the previous command succeeded.

The IF command is what makes this conditional branching possible.

This article explains the IF command from the basics through advanced usage, with practical sample code.

Basic Syntax of the IF Command

The basic syntax of the IF command is as follows.

IF condition (
    rem Commands to run when condition is true
)

To also handle the false case, add an ELSE clause.

IF condition (
    rem Commands for the true case
) ELSE (
    rem Commands for the false case
)
Syntax Note

ELSE must be written on the same line as the preceding closing parenthesis ). Placing ELSE on a new line will not work correctly.

Comparing Strings

To check whether two strings are equal, use the == operator.

IF "%variable%"=="string to compare" (
    rem Commands when they match
)
チェック

It is recommended to wrap values in double quotes. This prevents syntax errors when a variable is empty.

Example

Branching based on user input.

@echo off
setlocal

set /p ANSWER=Continue? (Y/N):

IF "%ANSWER%"=="Y" (
    echo Continuing the process.
) ELSE (
    echo Process cancelled.
)

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>string_compare.cmd
Continue? (Y/N): Y
Continuing the process.
C:\users\user>

Case-Insensitive Comparison

Use the /I option to compare strings without regard to case.

IF /I "%ANSWER%"=="y" (
    echo Continuing the process.
)

With this, both Y and y will match.

Comparing Numbers

For numeric comparisons, use these dedicated operators.

OperatorMeaningExample
EQUEqualIF %A% EQU 10
NEQNot equalIF %A% NEQ 0
LSSLess thanIF %A% LSS 100
LEQLess than or equalIF %A% LEQ 100
GTRGreater thanIF %A% GTR 0
GEQGreater than or equalIF %A% GEQ 1

Example

@echo off
setlocal

set /a NUM=75

IF %NUM% GEQ 80 (
    echo Excellent pass
) ELSE IF %NUM% GEQ 60 (
    echo Pass
) ELSE (
    echo Fail
)

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>number_compare.cmd
Pass
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

ELSE IF Chains

As shown above, chaining ELSE IF statements allows you to evaluate multiple conditions in order. Only the first matching block is executed.

Checking Whether a File or Folder Exists

IF EXIST checks whether a specified file or folder exists.

IF EXIST "path" (
    rem Commands when it exists
)

Example

@echo off
setlocal

IF EXIST "C:\Users\user\Documents\report.txt" (
    echo File found.
) ELSE (
    echo File does not exist.
)

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>check_file.cmd
File found.
C:\users\user>

Checking for a Folder

To check specifically for a folder, append \ to the path or use NUL.

IF EXIST "C:\Users\user\Documents\" (
    echo Folder exists.
)

Checking Whether a Variable Is Defined

IF DEFINED checks whether a variable has been set.

IF DEFINED variablename (
    rem Commands when the variable is defined
)

Example

@echo off
setlocal

set MY_VAR=Hello

IF DEFINED MY_VAR (
    echo MY_VAR is defined: %MY_VAR%
) ELSE (
    echo MY_VAR is not defined.
)

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>check_defined.cmd
MY_VAR is defined: Hello
C:\users\user>

Checking ERRORLEVEL

To check the exit code of the previous command, use IF ERRORLEVEL or %ERRORLEVEL%.

IF ERRORLEVEL 1 (
    rem Commands when exit code is 1 or higher
)
ERRORLEVEL Behavior

IF ERRORLEVEL N is true when ERRORLEVEL is N or higher. To check for an exact value, use IF %ERRORLEVEL% EQU N.

Example

@echo off
setlocal

copy "source.txt" "dest.txt" >nul 2>&1

IF %ERRORLEVEL% EQU 0 (
    echo Copy succeeded.
) ELSE (
    echo Copy failed. Error code: %ERRORLEVEL%
)

endlocal

Negating a Condition with NOT

NOT inverts the condition.

IF NOT EXIST "backup\" (
    mkdir backup
    echo Created backup folder.
)

IF NOT "%USERNAME%"=="admin" (
    echo Not an administrator.
)

IF NOT DEFINED CONFIG_PATH (
    set CONFIG_PATH=C:\default\config.ini
)

Combining Multiple Conditions

Batch files do not have AND or OR operators, but nested IF statements achieve the same result.

AND Condition (both conditions must be true)

IF EXIST "data.txt" (
    IF %ERRORLEVEL% EQU 0 (
        echo All conditions met.
    )
)

OR Condition (either condition is true)

set MATCH=0
IF "%INPUT%"=="A" set MATCH=1
IF "%INPUT%"=="B" set MATCH=1
IF %MATCH% EQU 1 (
    echo A or B was entered.
)

Summary

This article covered how to use the IF command.

Condition typeSyntax
String comparisonIF "%A%"=="%B%"
Case-insensitiveIF /I "%A%"=="%B%"
Numeric comparisonIF %A% EQU %B%
File existsIF EXIST "path"
Variable definedIF DEFINED varname
Error levelIF %ERRORLEVEL% EQU 0
NegationIF NOT condition

Mastering the IF command gives you much more flexibility and control over your batch files.

In preparation
The linked page is currently under construction. Please wait a moment.

Practice Quiz

Practice Problem

Choose the incorrect description about the IF command.

回答がサーバーに送信されることはありません
#Command Prompt #Batch File #IF #Conditional #Command