IF Statement and IF ELSE Statement in Batch Scripting
When writing Batch Scripts, we often need to make decisions based
on conditions. This is where the IF and IF ELSE statements are used.
They
allow a script to perform different actions depending on whether a condition
is true or false.
In this tutorial, we will learn how to use IF and
IF ELSE statements with practical examples.
What is an IF Statement?
An IF statement executes a block of code only when a specific condition
is true.
Syntax
if condition (
command
)
Example 1: Basic IF Statement
@echo off goto:main :main set /A number=5 if %number% equ 5 (echo This is IF statement) goto:eof
Output
This is IF statement
Explanation
In this example:
- A variable named number is created.
- The value of number is set to 5.
- The IF statement checks whether the value equals 5.
- Since the condition is true, the message is displayed.
The keyword:
equ
means equal to.
Example 2: Checking a User Path
@echo off
goto:main
:main
set /P path= Enter path
if %path% EQU C:\Users\user\Desktop\batch-scripting (
cd %path%
dir
)
goto:eof How It Works
User enters a path.
Batch compares the entered value with the
specified path.
If both match:
- The script changes to that directory.
- The contents of the folder are displayed using dir.
Sample Input
C:\Users\user\Desktop\batch-scripting
Sample Output
Directory of C:\Users\user\Desktop\batch-scripting
This
type of validation is commonly used in automation scripts.
What is an IF ELSE Statement?
The IF ELSE statement allows us to execute one block of code when a
condition is true and another block when the condition is false.
Syntax
if condition (
commands
) else (
commands
)
Example: IF ELSE Statement
@echo off
goto:main
:main
set /P path= Enter path
if %path% EQU C:\Users\user\Desktop\batch-scripting (
cd %path%
dir
)
else (
echo Invalid path
)
goto:eof
Explanation
The script checks the path entered by the user.
If the path is correct
C:\Users\user\Desktop\batch-scripting
The script:
- Changes to the directory
- Displays folder contents
- If the path is incorrect
- D:\Projects
The script displays:
Invalid path
This makes scripts more user-friendly because they can handle both valid and invalid inputs.
Common Comparison Operators
Example
if %number% GTR 10 (
echo Number is greater than
10
)
Real-World Uses of IF Statements
System Engineers and IT Administrators commonly use IF statements for:
- Checking whether a file exists
- Validating folder paths
- Verifying user input
- Monitoring services
- Automating troubleshooting tasks
- Running backup scripts
Interview Questions
What is an IF statement in Batch Scripting?
An IF
statement executes commands only when a specified condition is true.
What is the purpose of ELSE?
ELSE executes an alternative block of code when the IF condition is
false.
What does EQU mean?
EQU stands for "Equal
To".
Which command is used to take user input?
set /P
Why are IF statements important?
They allow scripts to make decisions and automate tasks based on
conditions.
Conclusion
The IF and IF ELSE statements are fundamental concepts in Batch
Scripting. They help automate decision-making and make scripts more
interactive. Whether you are validating user input, checking files, or
automating system administration tasks, conditional statements are an
essential skill for every Batch Scripting learner.






































