OK. You want to run a command a certain way if it is between 9 AM and 12 PM. You want to run it a different way if it is between 12 PM and 3 PM. How do you accomplish this with a batch file?
The answer is nested IF statements and the %time% system variable. The %time% variable stores the time in a 24 hour format including hour, minute, second and millisecond:
This represents 9:02 AM at 45.80 seconds into the minute. I prefer to work with just the hour in situations like this. So I create a variable called %hour% for such batch files using the following command:
The following shows this operation at the Windows command line:
Extracting the hour from the time variable
It is also important to know that you have operator keywords used with the IF statements. They are as follows:
- EQU – equal
- NEQ – not equal
- LSS – less than
- LEQ – less than or equal
- GTR – greater than
- GEQ – greater than or equal
Using these keywords can be simpler to read than the traditional == or != operator types for the uninitiated who read your batch file.
With this in mind, the following batch file code will allow you to test for the hour range and run commands only during specified hours:
IF %hour% GEQ 9 IF %hour% LEQ 12 (GOTO RUNIT)
ECHO Wrong time to run!
ECHO Right time to run!
Hopefully this helps you in such scenarios.
Here we are in 2017 and as I think about the past I realize that the future still holds much valuable use of batch files. Microsoft introduced the VBS-based Windows Scripting Host (WSH) in the late 90s. Then PowerShell and yet Command Prompt commands and, therefore, batch files still live on. The simple fact is that batch files are still much easier to create and edit than WSH scripts or PowerShell scripts. This does not mean that PowerShell lacks value. It simply means that, when you want a quick solution to automation, batch files work well – usually very well.
Just a few example uses of batch files are:
- Automating local user account and group management with NET USER and NET GROUP
- Backing up closed files to an external drive or network share
- Generating reports related to network operations
- Configuring complex networking parameters automatically
Many, many more could be listed. To prove that the Command Prompt really isn’t going anywhere, Microsoft gave us a new tool with Windows 10 called MpCmdRun.exe. This command is used to execute Windows Defender scans from the Command Prompt. Adding yet another useful batch file example, malware scanning.
For example, the simple command mpcmdrun -scan -scantype 1 executes a quick scan from the Command Prompt. The command provides a return code of 0 when no malware is found. Consider, then, the coupling of the previous command with an if errorlevel == 0 (echo Success) else (echo Failure). The end result is the output of the word Success when no malware is found or Failure when malware is found.
Now, this example is not extravagant, but imagine if the if statement called other commands or applications to take action. As you can see, indeed, batch files are still valuable and powerful. Even in Windows 10, the latest and greatest from Microsoft.