5 IF Statements to Use for Smarter Windows Batch Scripts

Posted on

If you do a lot of work in Windows batch files, the IF statement offers a very powerful way to add flexibility to your scripts.

In this article you’re going to learn about the five main types of IF statements you can use in a Windows batch file, how the correct syntax looks, and a realistic example for each.

If you’re ready to start scripting, let’s get started! computer science computer science computer science

1. Compare Values

One of the basic things you’ll usually need to do in a batch script is compare two valuesand follow a different course of action depending on the comparison. computer science computer science computer science computer science

For example, let’s say you wanted to write a batch script that checks your computer’s hard drive size daily. If it’s below 3 GB you want to get an email report that says, “Hard Drive Space Too Low.”

To create a script that compares the current free hard drive space to your limit, you’d create the following batch script and save it as a .bat file.computer science computer science computer science

@echo off
set DriveLimit=300000000
for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:
'" get FreeSpace /format:value`) do set FreeSpace=%%x
Echo FreeSpace="%FreeSpace%"
Echo Limit="%DriveLimit%"
If %FreeSpace% GTR %DriveLimit% (
 Echo There is enough free space.
) else (
 Echo Not enough free space.
)

WMIC is the Windows Management Instrumentation component of Windows that comes with an assortment of commands you can use to pull PC information. This is how the “wmic” command in this script calls the “logicaldisk” space and places it into the FreeSpace variable. Now you can just replace the line “Echo Not enough free space” with a blat email command to send you an alert.

Finally, set this script up as a windows scheduled batch job that runs daily.

If you’ve never used blat before, we have an article that shows you how to set up blat. Unfamiliar with setting up scheduled jobs? We’ve got you covered with an article on how to set up Windows scheduled tasks.

2. String Comparisons

Another valuable IF comparison you can do in a batch job is comparing strings.

In the following example you’ll see how to check your Windows version using a batch job. Then you can compare this to your expected Windows version.  computer science computer science  computer science computer science

Some uses of this script would be for IT audits when you need to quickly run a script and make sure the current operating system is the latest, or whether it needs an upgrade.

Here’s what this script looks like.

@echo off
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version%" == "6.0" echo Windows Vista.
if "%version%" == "6.1" echo Windows 7
if "%version%" == "6.2" echo Windows 8
if "%version%" == "6.3" echo Windows 8.1
if "%version%" == "10.0" echo Windows 10.

Here’s what the output of this script looks like. computer science computer science computer science

The ability to compare strings in batch opens up a whole list of possibilities. If you explore all of the information you can obtain out of a WMIC command you’ll see just how many statistics about your computer you can monitor. You can use scheduled batch jobs to alert on these.

Prev1 of 3Next

Leave a Reply

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