问题
I have a bat file that I want to run at specific day/time in each quarter for a year. I m new to command line and I know how to run it via a task scheduler, which is very easy.
Let's say for each quarter, for 1st and 2nd month, my bat file should run at alternative weeks. For the third month of a quarter, it should run weekly. Possible time would be early morning 6:00 a.m. How would I do it? Please let me know. Thanks.
回答1:
There's the windows scheduler, but I doubt you are going to be able to do a schedule that advanced with just the windows scheduler.
What I would do is write the scheduling logic (e.g. when you want your batch file to run) in some high level language (C#, JAVA, etc.), then call that program at the beginning of your batch file to see if it's a date you care about.
The batch file could be set up to run each day (or multiple times per day if needed) using the windows scheduler, but it would only do the "real stuff" if your C#/JAVA program indicated that it was a date you cared about.
回答2:
The Batch file below do what you want:
@echo off
rem Get the "monthInQuarter@weekInMonth" value of the last run:
set /P lastRun=< scheduler.txt
rem Get values from today date
for /F "tokens=1,2 delims=/" %%a in ("%date%") do (
set /A "monthInQuarter=(1%%a-100)%%4, weekInMonth=(1%%b-101)/7+1, oddWeekInMonth=weekInMonth%%2"
)
if %weekInMonth% gtr 4 (
set /A weekInMonth=4, oddWeekInMonth=0
)
set thisRun=%monthInQuarter%@%weekInMonth%
rem For 1st and 2nd month in each quarter:
if %monthInQuarter% leq 2 (
rem For alternative weeks (1=yes, 2=no, 3=yes, 4=no):
if %oddWeekInMonth% equ 1 (
if "%thisRun%" neq "%lastRun%" (
echo %thisRun%> scheduler.txt
call :TheProcess
)
)
rem For the third month of a quarter:
) else if %monthInQuarter% equ 3 (
rem Run it weekly:
if "%thisRun%" neq "%lastRun%" (
echo %thisRun%> scheduler.txt
call :TheProcess
)
)
exit
:TheProcess
rem Place here the desired process, ie:
echo This run only on selected weeks!
exit /B
This table of values may help to understand the scheduling logic:
month: 1 2 3 4 5 6 7 8 9 10 11 12
monthInQuarter: 1 2 3 0 1 2 3 0 1 2 3 0
day: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
weekInMonth: 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 ...
oddWeekInMonth: 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 ...
Notes:
This Batch file assume that ECHO %DATE% command show the date in MM/DD/YYYY format with a left zero if month or day is less than 10. If this is not your locale date format, a slight modification is needed.
Although it is possible to modify this program to work with any date format, I think this is NOT a general-use file, but a very particular request for your specific needs.
This program must run daily at 6:00 am via task scheduler.
The first time the program run "The system cannot find the file specified." message appear, before auxiliary SCHEDULE.TXT file be created.
I hope it helps.
Antonio
PS - I don't think this problem can be solved in an easier way via vbs script or C#, JAVA, etc...
来源:https://stackoverflow.com/questions/11019792/how-to-run-a-bat-file-at-specified-day-time