Author Archives: admin

Execute Batch File Commands During Specified Hours

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:

SET “hour=%time:~0,2%”

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:

SET hour=%time:~0,2%
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.

Execute a Batch File Only on A Specific Day

Have you ever wanted to run a batch file on a specific day, week, month, hour or minute? If so, you can do this with the Windows command line using the %date% or %time% system (environment) variables. To see the contents of these variables, execute the ECHO %time% and ECHO %date% commands at the Windows command line. The following screen shows the data stored in %date% and %time%.

ECHO DATA and TIME Variable

The ~ operator can be used to select a substring from such a variable. Notice that the day of the month is the 8th and 9th characters of the %date% contents at all times. Therefore you can go to the 7th character and then grab the next two and know that you’ve always gotten the day of the month (with days 1-9 represented as 01, 02, 03 …, 09).

With this information, considering the following batch file that uses the IF command and takes advantage of the ~ operator within the %date% variable:

IF %date:~7,2%==13 (GOTO :run_it) else (GOTO :no_run)

REM run whatever command
ECHO Running the command...

REM Notify user that it is the wrong day
ECHO Wrong day!


If this batch file is executed on a 13th day of any month, the DIR C:\ command will be executed. Otherwise, the notification of “Wrong day!” will be displayed for the user. From this example, you should be able to work out methods for determining the day of the month, month of the year and even hour of the day or minute of the hour.

Batch Files Are Still Valuable

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.

MpCmdRun.exe example


The Command Prompt Lives

The Windows Command Prompt, which is the proper name of the Windows command line interface, is still wildly popular. Did you know that more than 1.5 million Google searches are performed each month related to Command Prompt commands? It is estimated that more than 2.5 million total searches are performed across all search engines. This shows a lot of interest in the windows command line and this site is here to help you learn and master each and every command.

The point is that the Windows command line is alive and well. The good news for those who may be old hats with the Disk Operating System (DOS) of years gone by is that the majority of the DOS commands still work in the Windows command line. In fact, the Command Prompt (which is launched with the CMD.EXE program) looks just like the old DOS interface.

In addition, you can still create batch files at the windows command line. These batch files still support the GOTO command and other logic constructions to create advanced scripts for automation of administration tasks. And, yes, you can still find programs that will compile the batch file into an executable so that others cannot easily tamper with your logic.

Now, if you’re completely new to the text-based interfaces, that’s no problem. This site will get you up to speed quickly. Here at, we are building a site that will teach you how to use every single command that comes with every version of Windows from Windows XP through to Windows 7 and even those commands that are only on the Windows Server platform. You’ll also learn about batch file programming, interacting with the command line and configuring the Command Prompt application to your liking.

Watch for content to start arriving in April. The site will have complete coverage of all commands by mid-Summer. Thanks for stopping by.

-Tom Carpenter