HOWTO · Batch
If ELSE Condition in Batch Script
Learn how to write IF ... ELSE conditions in Windows CMD batch files, compare values, nest conditions, and check files.
On this page
Use IF ... ELSE in a Windows CMD Batch Script to run one command group when a condition is true and another when it is false. Put each group in parentheses, and place ELSE on the same logical command line as the closing parenthesis of the true branch. The examples below are batch-file examples. Their output reflects the inputs and environment shown.
IF … ELSE Syntax and a Basic Example
The general form is IF condition ( commands-for-true ) ELSE ( commands-for-false ). The condition can test a comparison or a file attribute. A batch file may omit ELSE when it needs to do nothing for the false case.
Run this example from a .bat file. It sets two values and compares them, then reports which branch is selected. Because 10 is less than 20, the true branch is selected; the output is:
@echo off
set /a left=10
set /a right=20
if %left% LSS %right% (
echo %left% is less than %right%.
) else (
echo %left% is not less than %right%.
)
10 is less than 20.
For a file check, use IF EXIST path. It selects the true branch when the named file or directory exists at the time of the check. The following self-contained batch file creates a temporary file, checks it, then removes it:
@echo off
set "sample=%TEMP%\if-else-sample-%RANDOM%.txt"
echo sample>"%sample%"
if exist "%sample%" (
echo File exists.
) else (
echo File does not exist.
)
del "%sample%"
File exists.
Change the path to check a different file. If you only need to handle a missing file, IF NOT EXIST path ( commands ) runs those commands when the path is absent.
Comparison Operators in Batch
For numeric conditions, CMD supports EQU (equal), NEQ (not equal), LSS (less than), LEQ (less than or equal to), GTR (greater than), and GEQ (greater than or equal to). These are numeric comparisons; use == to compare strings. Quote string values so spaces and an empty value do not break the condition, for example: IF "%mode%"=="backup" ( echo Backup mode ).
NOT negates a condition, as in IF NOT EXIST "report.txt" ( echo Create the report first ). AND and OR are not direct logical operators in CMD’s IF condition syntax. To require multiple checks, nest IF statements; to run another command after a successful command, CMD’s && command chaining is a separate feature and has different behavior from combining conditions.
Nested IF … ELSE Conditions
Place another IF ... ELSE inside a branch when the next decision depends on the first. This complete example checks the higher grade threshold first, then the next threshold:
@echo off
set /a score=85
if %score% GEQ 90 (
echo Grade: A
) else (
if %score% GEQ 80 (
echo Grade: B
) else (
echo Grade: C
)
)
The first condition is false for 85, so CMD evaluates the nested condition. Since 85 is at least 80, that branch prints the B grade. The order matters: checking the lower threshold first would also match higher scores.
Practical IF … ELSE Examples
A file check is useful before reading, copying, or deleting a path. For example, IF EXIST "%~1" ( ... ) ELSE ( ... ) checks the first argument passed to a batch file. %~1 removes surrounding quotes from that argument; quoting it again in the condition handles paths containing spaces.
A script can also attempt an administrative operation and inspect its result, but a single privilege probe is not universally reliable: the command may fail because of service configuration or policy as well as permissions. In a Windows CMD batch file, the pattern is net session >nul 2>&1 followed immediately by IF ERRORLEVEL 1 ( echo The command did not succeed; check permissions and system configuration. ) ELSE ( echo The command succeeded. ). This platform-specific command depends on Windows services and account policy, so it is described here without an asserted runtime output or privilege guarantee.
IF ERRORLEVEL n checks whether the preceding command returned an exit code greater than or equal to n. The test must follow the command whose status you want to inspect. Do not assume every failure means the account lacks administrative privileges.
Common Mistakes and Windows CMD Scope
These examples use Windows CMD Batch Script syntax, not PowerShell or Bash syntax. Keep ELSE attached to the closing parenthesis of the true command group; inserting a command separator or starting ELSE on a separate command line can prevent CMD from pairing the branches.
CMD expands %variable% when it reads a parenthesized block. If a variable changes inside that block, a later %variable% reference in the same block may still have its earlier value. In a script that needs the updated value inside a block, enable delayed expansion with setlocal EnableDelayedExpansion and refer to it as !variable!; remember that ! can itself matter in paths or text when delayed expansion is active.
Do not use AND or OR as if they combine two IF comparisons. Nest the conditions to express dependent checks, and make sure each ELSE belongs to the intended IF. For string comparisons, quote both sides, such as IF "%first%"=="%second%", so spaces or a missing value do not alter how CMD parses the comparison.