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%.
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) :run_it CLS REM run whatever command ECHO Running the command... DIR C:\ GOTO END :no_run REM Notify user that it is the wrong day ECHO Wrong day! :END
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.