if - Conditional Branching
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
)
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
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.
| Operator | Meaning | Example |
|---|---|---|
EQU | Equal | IF %A% EQU 10 |
NEQ | Not equal | IF %A% NEQ 0 |
LSS | Less than | IF %A% LSS 100 |
LEQ | Less than or equal | IF %A% LEQ 100 |
GTR | Greater than | IF %A% GTR 0 |
GEQ | Greater than or equal | IF %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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
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
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
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
)
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 type | Syntax |
|---|---|
| String comparison | IF "%A%"=="%B%" |
| Case-insensitive | IF /I "%A%"=="%B%" |
| Numeric comparison | IF %A% EQU %B% |
| File exists | IF EXIST "path" |
| Variable defined | IF DEFINED varname |
| Error level | IF %ERRORLEVEL% EQU 0 |
| Negation | IF NOT condition |
Mastering the IF command gives you much more flexibility and control over your batch files.