簡體   English   中英

如何在指定的日期/時間運行 bat 文件?

[英]How to run a bat file at specified day/time?

我有一個 bat 文件,我想在一年中每個季度的特定日期/時間運行。 我是命令行的新手,我知道如何通過任務調度程序運行它,這非常簡單。

假設每個季度,第 1 個月和第 2 個月,我的 bat 文件應該在不同的星期運行。 對於一個季度的第三個月,它應該每周運行一次。 可能的時間是清晨 6:00 我該怎么做? 請告訴我。 謝謝。

下面的批處理文件可以滿足您的要求:

@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

此值表可以幫助您了解調度邏輯:

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 ...

筆記:

  • 該批處理文件假定ECHO%DATE%命令以MM / DD / YYYY格式顯示日期,如果月份或日期小於10,則以左零表示。如果這不是您的區域設置日期格式,則需要進行一些修改。

  • 盡管可以修改此程序以使其適合任何日期格式,但我認為這不是通用文件,而是針對您特定需求的非常特殊的請求。

  • 該程序必須每天早上6點通過任務計划程序運行。

  • 程序第一次運行“系統找不到指定的文件”。 在創建輔助SCHEDULE.TXT文件之前,出現此消息。

希望對您有所幫助。

安東尼奧

PS-我認為無法通過vbs腳本或C#,JAVA等以更簡單的方式解決此問題...

Windows Scheduler ,但是我懷疑您是否能夠僅使用Windows Scheduler進行高級計划。

我要做的是用某種高級語言(C#,JAVA等)編寫調度邏輯(例如,當您希望運行批處理文件時),然后在批處理文件的開頭調用該程序以查看它是否是您關心的日期。

可以使用Windows調度程序將批處理文件設置為每天運行(如果需要,可以每天運行多次),但是只有在您的C#/ JAVA程序指示您關心的日期時,它才會執行“實際工作” 。

public static void main(String[] args) 拋出異常 {

  String hours=PropertyUtil.getProperty("START_HOURS");
 String minutes=PropertyUtil.getProperty("START_MINUTES");
  String hours1=PropertyUtil.getProperty("SECOND_START_HOURS");
     String minutes1=PropertyUtil.getProperty("SECOND_START_MINUTES");
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hours));
    calendar.set(Calendar.MINUTE, Integer.parseInt(minutes));
    calendar.set(Calendar.SECOND, 00);
     Long currentTime = new Date().getTime();
     System.out.println("Current time is : "+currentTime);
     //Check if current time is greater than our calendar's time. If So, then change date to one day plus. As the time already pass for execution.
     if (calendar.getTime().getTime() < currentTime) {
            calendar.add(Calendar.DATE, 1);
        }


    Date time = calendar.getTime(); 
    long  period = 1000L  60L  60L * 24L; // 24 Hours
    System.out.println(" First run  time is : "+time);
    System.out.println("milli seconds: "+period);
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            
            
            try {
            System.out.println("  Service Started...."+new Date());
                //main logic
            System.out.println("service completed...."+new Date());
            } catch (Exception e) {
             e.printStackTrace();
            }
            
        }
    }, time, period);
    Calendar calendar1 = Calendar.getInstance();
    calendar1.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hours1));
    calendar1.set(Calendar.MINUTE, Integer.parseInt(minutes1));
    calendar1.set(Calendar.SECOND, 00);
    //Check if current time is greater than our calendar's time. If So, then change date to one day plus. As the time already pass for execution.
    if (calendar1.getTime().getTime() < currentTime) {
        calendar1.add(Calendar.DATE, 1);
    }
    Date time1 = calendar1.getTime();   
    System.out.println("Second run Time is : "+time1);
    System.out.println("Milli seconds : "+period);
    Timer timer1 = new Timer();
    timer1.schedule(new TimerTask() {
        @Override
        public void run() {
            
            
            try {
            System.out.println("  Service Started...."+new Date());
                //main logic
            System.out.println("service completed...."+new Date());
            } catch (Exception e) {
             e.printStackTrace();
            }
            
        }
    }, time1, period);
                    

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM