Introduction: Batch Scripting : Using 'If Statements' for Decision Making

About: Hey there, I'm Shabana, and I'm all about the fun mix of cooking, fashion makeovers, and clever computer tricks. Imagine whipping up tasty dishes, giving a fresh twist to your wardrobe, and making life simpler…

Welcome to the world of batch scripting! In this guide, you'll learn how to use "if statements" in batch scripts.

Batch scripts are like simple computer programs that can help you automate tasks. "If statements" are a way to make your scripts smart. They allow your script to make decisions based on certain conditions.

In this tutorial, we'll show you how to set up these conditions and make your script do different things depending on what's happening.

By the end of this guide, you'll be able to make your batch scripts more powerful and flexible with "if statements." Let's get started!

Supplies

Here are the tools you'll need for creating a batch file:

  1. Text Editor: You can use the built-in Windows Notepad or any other text editor of your choice to create and edit your batch script.
  2. Windows Command Prompt: To run and test your batch script, you'll use the Windows Command Prompt, which is a built-in command-line interface in Windows.

That's all you need to get started with your batch scripting with "if statements.

Step 1: Open the Windows Start Menu

To get started with creating and running batch scripts, you'll need to access the Windows Start menu. Here's how to do it:

  1. Locate the Windows icon:- In the bottom-left corner of your screen, you'll find the Windows icon, which represents the Start menu. It's typically located in the taskbar.
  2. Click the Windows icon:- Simply left-click on the Windows icon, and this will open the Start menu.

By opening the Start menu, you'll be able to access various Windows tools and applications, including the Command Prompt, which you'll use to run your batch scripts in the subsequent steps.

Step 2: Open Notepad and Write Your Batch Script

In this step, you'll open Notepad and write your batch script using "if statements." Here's how to do it:

  1. Open Notepad: You can open Notepad by pressing the Windows key, typing "Notepad," and pressing Enter. This will launch the Notepad text editor.
  2. Write Your Batch Script: In Notepad, type your batch script. @echo off

@echo off : When you include @echo off at the beginning of your batch script, you're essentially telling the script not to show the commands it's executing on the screen. It's like running your script in "silent" mode, so the user doesn't see all the technical details, making the output cleaner and easier to understand.

Step 3: "set" Command

In batch scripting, the set command is used to create and assign values to variables. Think of variables as containers where you can store information or data for later use in your script. The set command allows you to give a variable a name and assign a value to it.

set number=15 create a variable named "number" and assign it the value "15".


Step 4: "if" Statement

"if" statement is like making a decision in your script. It allows your script to check if a certain condition is true or false, and then perform different actions based on that condition.

if %number% here if statement and variable

next "gtr" The "gtr" in the line if %number% gtr 10 is a comparison operator used in batch scripting. It stands for "greater than."

Here are some common comparison operators along with their meanings:


  • equ (Equal): Checks if two values are equal. For example, if %var1% equ %var2%.
  • neq (Not Equal): Checks if two values are not equal. For example, if %var1% neq %var2%.
  • lss (Less Than): Checks if one value is less than another. For example, if %var1% lss %var2%.
  • leq (Less Than or Equal To): Checks if one value is less than or equal to another. For example, if %var1% leq %var2%.
  • geq (Greater Than or Equal To): Checks if one value is greater than or equal to another. For example, if %var1% geq %var2%.

You can use these operators to compare variables or values in your "if" statements and execute different commands based on the comparison results. These operators provide the flexibility to create conditional logic in your batch scripts.



Step 5: Echo

The echo command in batch scripting is used to display messages or output text in the console

echo The number is greater than 10.

Step 6: Else

  • The else statement is like a backup plan in your batch script.
  • It's used to decide what to do if the main condition (in the if statement) is not met.
  • Think of it like this: "If the main condition is true, do this (inside the if block). If it's not true, do this other thing (inside the else block)."
  • It's a way to handle different situations or outcomes in your script, depending on whether the main condition is true or false.

in else statement we don't write any condition only write echo.

echo The number is not greater than 10. and pause

The" pause "command is useful for keeping the console window open after the script finishes so that the user can see the script's output and any messages before the console window closes.


Step 7: Save the File

  • After writing your script, save it by clicking on "File" in Notepad and selecting "Save." and name it "your file name" file name is upto you but The .bat extension is important for batch scripts. Close Notepad after saving your batch script, you can double-click the file to run it. Here's what happens when you double-click the batch script file:
  • The script file is executed, and the commands within it are processed by the Command Prompt.
  • echo commands, pause commands, results displayed in the Command Prompt window.
  • If you included a pause command at the end of your script, the Command Prompt window will stay open until you press a key.
  • Once you press a key (if there's a pause command), the Command Prompt window will close.

you're done.