Using if condition in CMD
Categories:
Mastering Conditional Logic with IF Statements in CMD Batch Files

Unlock the power of decision-making in your Windows batch scripts using the IF command. This guide covers basic syntax, common comparisons, and advanced conditional structures.
Conditional statements are fundamental to any programming or scripting language, allowing your code to make decisions and execute different blocks of instructions based on specific criteria. In Windows Command Prompt (CMD) batch files, the IF command serves this crucial purpose. Understanding how to effectively use IF statements will significantly enhance the flexibility and power of your batch scripts, enabling them to respond dynamically to various situations.
Basic IF Statement Syntax
The simplest form of the IF statement in CMD checks a condition and executes a command if that condition is true. The basic syntax is straightforward:
IF [condition] command
If the condition evaluates to true, the command is executed. If it's false, the command is skipped, and the script continues to the next line. You can also use GOTO to jump to a specific label within your script based on a condition.
@echo off
SET /P USER_INPUT="Enter 'yes' or 'no': "
IF /I "%USER_INPUT%"=="yes" ECHO You entered yes.
IF /I "%USER_INPUT%"=="no" ECHO You entered no.
ECHO Script finished.
Basic IF statement checking user input.
Comparing Strings and Numbers
The IF command supports various comparison operators for both strings and numbers. It's important to use the correct syntax for each type of comparison.
String Comparisons
For string comparisons, you typically use == to check for equality. It's good practice to enclose variables in double quotes to prevent issues with spaces or empty values. The /I switch makes the comparison case-insensitive.
Numeric Comparisons
For numeric comparisons, CMD provides specific operators:
EQU(Equal)NEQ(Not Equal)LSS(Less Than)LEQ(Less Than or Equal)GTR(Greater Than)GEQ(Greater Than or Equal)
These operators are used with the IF command in the format IF %variable1% operator %variable2% command.
@echo off
SET MY_STRING="Hello World"
SET MY_NUMBER=10
REM String comparison (case-sensitive)
IF "%MY_STRING%"=="Hello World" ECHO String matches exactly.
REM String comparison (case-insensitive)
IF /I "%MY_STRING%"=="hello world" ECHO String matches case-insensitively.
REM Numeric comparison
IF %MY_NUMBER% GTR 5 ECHO Number is greater than 5.
IF %MY_NUMBER% LEQ 10 ECHO Number is less than or equal to 10.
ECHO Comparisons complete.
Examples of string and numeric comparisons.
IF statements (e.g., "%VAR%"=="value") to prevent errors if the variable contains spaces or is empty. This makes your scripts more robust.Conditional Execution Blocks with IF ELSE
For more complex logic, you often need to execute one set of commands if a condition is true and another set if it's false. CMD supports IF ELSE structures, which can be nested for intricate decision trees. The syntax involves using parentheses to group commands.
IF [condition] ( command1 ) ELSE ( command2 )
This structure allows for clear branching in your script's execution path.
@echo off
SET /P AGE="Enter your age: "
IF %AGE% GEQ 18 (
ECHO You are an adult.
) ELSE (
ECHO You are a minor.
)
ECHO Decision made.
Using IF ELSE for branching logic.
flowchart TD
A[Start]
B{Is Age >= 18?}
C[Echo "You are an adult."]
D[Echo "You are a minor."]
E[End]
A --> B
B -- Yes --> C
B -- No --> D
C --> E
D --> EFlowchart illustrating the IF ELSE decision process.
Checking for File and Directory Existence
The IF command can also check for the existence of files and directories, which is incredibly useful for validating paths or ensuring resources are available before proceeding.
IF EXIST [path]checks if a file or directory exists.IF NOT EXIST [path]checks if a file or directory does not exist.IF EXIST [directory\]specifically checks for a directory (note the trailing backslash).IF EXIST [file.ext]specifically checks for a file.
@echo off
SET FILE_PATH="C:\Windows\System32\notepad.exe"
SET DIR_PATH="C:\Program Files"
SET NON_EXISTENT_FILE="C:\nonexistent.txt"
IF EXIST %FILE_PATH% (
ECHO Notepad.exe exists.
) ELSE (
ECHO Notepad.exe does NOT exist.
)
IF EXIST %DIR_PATH%\ (
ECHO Program Files directory exists.
) ELSE (
ECHO Program Files directory does NOT exist.
)
IF NOT EXIST %NON_EXISTENT_FILE% (
ECHO Nonexistent.txt does NOT exist (as expected).
)
ECHO File/directory checks complete.
Checking for file and directory existence using IF EXIST.
IF EXIST C:\MyFolder\). Without it, CMD might interpret C:\MyFolder as a file named MyFolder in the C:\ directory.