Write Batch Files in Windows? How If-Else Statements Work

Ben Stegner 12-10-2017

Batch files are one of Windows’ hidden secrets for productivity. With just a bit of work, you can automate monotonous tasks How to Use Windows Batch File Commands to Automate Repetitive Tasks Do you frequently execute boring and repetitive tasks? A batch file might be exactly what you’re looking for. Use it to automate actions. We'll show you the commands you need to know. Read More and never worry about them again. Power users should learn all about batch scripting sooner rather than later.


One thing you’ll need to know when writing your first batch files How to Create a Batch (BAT) File in Five Simple Steps This article covers how to create a batch file using five simple steps for the Windows operating system. Read More is the if-else statement. As you might know if you have any programming experience, the if-else statement is a way to control scripting logic. It allows you to specify a condition that branches into different blocks of code.

If the condition is true, the code in the if block executes. Otherwise, the code in the else block runs instead. Here’s a basic example to help you visualize it:

if (cash > 10) output "You have enough money!";
else output "You don't have enough money.";

The following syntax is how to use the if-else statement in batch files:

if %x%==15 (echo "The value of x is 15") else (echo "Unknown value") 

This example works when you are expecting a certain input and anything else can output a standard message. But there’s another scenario that has a different solution.

In most programming languages, you can use a a modified form of the if-else statement. Adding else if in the second statement allows you to execute a different block of code if the initial statement is false but a second condition returns true, like in this modified example:

if (cash > 20) output "You have lots of money!";
else if (cash > 10) output "You have enough money!";
else output "You don't have enough money.";

However, you can’t use else if in batch scripting. Instead, simply add a series of if statements:

if %x%==5 if %y%==5 (echo "Both x and y equal 5.")
if %x%==10 if %y%==10 (echo "Both x and y equal 10.") else (echo "Invalid input.")

Interested in more? Check out why PowerShell might be even better than batch scripting 5 Reasons You Should Use PowerShell Instead of Batch Scripting PowerShell is what you'd get if you crossed the Command Prompt with Batch Scripting, threw in some extra features, and kicked it all up several notches. Here are several reasons you should try it. Read More .

Have you ever used if-else statements in batch scripting? What do you use batch files to accomplish? Share with us in the comments!

Image Credit: Aaban via

Related topics: Batch File, Scripting.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *