5 IF commands help use Batch script smarter
If you have to do a lot of work with Windows batch files, the IF command is a very powerful tool to increase the flexibility of these scripts. This article will introduce you to the 5 IF commands used for batch files along with the exact syntax and specific examples for each command.
If you have to do a lot of work with Windows batch files, the IF command is a very powerful tool that increases the flexibility of these scripts. This article will introduce you to the 5 IF commands used for batch files along with the exact syntax and specific examples for each command.
- These "hack" tips are only Notepad can do
- Troll friends by creating "fake" virus on Notepad
1. Compare values
One of the basic things a user usually does in a batch script is to compare two values and follow a series of different actions depending on the comparison.
For example, suppose you want to write a batch script to check the size of your computer's hard drive daily. If the hard drive has a capacity of less than 3 GB, you want to receive an email report saying "Hard disk space is too low".
- Some tips to free up Windows 10 computer hard drive space
To create a script that compares the free space on a hard drive with limited space, copy the following batch script and save it as a .bat file.
@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, accompanied by a series of commands used to retrieve computer information. This is how the command 'wmic' takes the space ' logicaldisk ' and places it in the FreeSpace variable. Now you just need to replace the " Echo Not enough free space " line with the blat email command to send an alert.
- Managing Windows networks using scripts - Part 3: Understanding WMI
Finally, set this script to batch job (a series of steps in a batch process) schedule the daily run.
2. Compare strings
Another job you can use the IF command in batch file is string comparison. In the example below, you will see how to check the version of Windows using a batch job. IT auditors use this script when they need to quickly run a script to check if the current operating system is the latest or needs to be upgraded.
Here is the script:
@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.
This is the output of this script.
If you look at all the information obtained from a WMIC command, you can select the computer monitoring information from this statistic, thereby using scheduled job batches to check this information.
- 4 simple ways to check the configuration and hardware information of computers and laptops
3. Check the existence of a file
Another useful case of the IF command in batch files is that it is used to check the existence of a data file. Sometimes the batch job is used as a monitoring tool, scheduled to check for new data files in a specific directory. Then, batch job will copy the file to another location to perform processing or launch some Windows scripts to process the file in Excel output.
- Hands-free with 5 VB scripts that automate the use of Windows computers
Using a batch file to check the existence of a file in the directory is easy and fast. This is the script used.
@echo off if exist c:tempdatafile.txt ( %WINDIR%SysWOW64cmd.exe cscript LoadToExcel.vbs ) else ( rem file doesn't exist )
Comparing IF EXISTS is very useful, for example, if there is a running system or application, creating new error logs in a specific directory when a problem occurs, you can run a batch job regularly. to track every time a new error log is created to send an alert.
4. Check that the execution command failed
One use of batch scripts that few IT people or programmers use is error checking. There are many batch jobs that perform important IT tasks such as backing up files or running file copying operations. When these job batches fail, the system fails and the user knows that a problem has occurred.
Therefore, you need to set up to receive notifications when the batch job fails to execute the command before an error occurs and the problem is known to be able to actively resolve the problem.
You can do this by using the % errorlevel% variable that most applications and commands return after they run. To use this variable, you only need to add the command after the I F% ERRORLEVEL% command. If the application or command returns a zero, it means that no problem occurs, otherwise you will receive a warning email.
@echo off xcopy C:somefolder E:backupfolder IF %ERRORLEVEL% NEQ 0 < blat command to send email >
You can write an error log to check each day or launch the application or the second command to copy using an alternate command. All of these tasks are performed by IF% ERRORLEVEL%.
5. Check the missing parameters
The last useful IF statement is not a specific command, but to check if the script receives the appropriate input parameters. For example, suppose you wrote a script that executed the xcopy command from an input folder to a shared network folder used by a group. Users only need to follow your script name with parameters that determine their personal file path.
Obviously, you cannot properly execute your script without the specified path, so you can put an IF statement at the top of the script to make sure that both parameters are entered. This is the command used:
@echo off IF [ %1 ] == [ ] ( GOTO sub_message ) ELSE ( xcopy %1 E:backupfolder ) GOTO eof :sub_message echo You forgot to specify your path. :eof
If you have never used parameters with the previous batch script, the percentage symbol followed by a number representing the parameter variable,% 1 is the first parameter,% 2 is the second parameter, etc.
Many people start using batch jobs for simple tasks that need to be performed in sequence. With the IF command you can make these tasks much more flexible and simple.
See more:
- Create your own system status check application with Windows Script and LiveGraph
- Instructions to send email automatically in Google Sheet with Google Script
- Script supports lyrics for YouTube
You should read it
- Automate TELNET commands using VB Script
- 5 software to rename files in batch
- How to Write a Batch File
- How to Delete a File in Microsoft Windows Using Batch Files
- How to batch rename files on Windows without software
- Instructions for creating and using BAT file on Windows
- How to Run a Batch File from the Command Line on Windows
- How to Run a BAT File on Windows
- Call command in Windows
- Top 5 software for fast batch file renaming
- How to batch delete files on Windows 10
- How to Launch BAT Files on Windows