Windows批次事件提醒第二天

如何编写一个批处理脚本,它将在此格式的Dates.txt文件中进行search:

EventName1:dd.mm.yyyy

EventName2:dd.mm.yyyy

EventNameN:dd.mm.yyyy

对于明天的事件,如果发现,通知用户关于他们?

我能够为今天的活动写一个脚本:

@echo off setlocal disableDelayedExpansion IF NOT EXIST Dates.txt GOTO not_found_dates for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j set ldt=%ldt:~6,2%.%ldt:~4,2%.%ldt:~0,4% echo Today: %ldt% for /f "tokens=1,2 delims=:" %%A in (Dates.txt) do ( if "%%B"==" %ldt%" echo You have %%Atoday! ) GOTO:EOF :not_found_dates echo Dates.txt not found! GOTO:EOF 

但我不知道如何find明天的date来比较它与文件中的date。

一些帮助将不胜感激!

Solutions Collecting From Web of "Windows批次事件提醒第二天"

那我自己终于明白了!

 @echo off setlocal disableDelayedExpansion IF NOT EXIST Dates.txt GOTO not_found_dates for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j set d=%ldt:~6,2% set m=%ldt:~4,2% set y=%ldt:~0,4% set ldt=%d%.%m%.%y% echo ************************ echo * Today: %ldt% * :loop set /a d+=1 if %d% gtr 31 ( set d=1 set /a m+=1 if %m% gtr 12 ( set m=1 set /a y+=1 ) ) xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 || goto loop set td=%d% IF %td% LSS 10 SET td=0%td% set tm=%m% IF %tm% LSS 10 SET tm=0%tm% set ty=%y% set tomorrow=%td%.%tm%.%ty% echo * Tomorrow: %tomorrow% * echo ************************ for /f "tokens=1,2 delims=:" %%A in (Dates.txt) do ( if "%%B"==" %tomorrow%" echo # You have %%Atomorrow! ) GOTO:EOF :not_found_dates echo Dates.txt not found! GOTO:EOF 

它适用于Dates.txt文件,该文件使用以下格式的日期:

EventName1:31.05.2016
EventName2:30.05.2016
EventName3:31.05.2016
EventName4:01.06.2016
EventName5:31.05.2016
EventName6:02.06.2016
EventName7:01.06.2016

(不要忘记冒号之前和之后的空白空间,以及关于几天和几个月的前导零不到10)

更新:

首先, set /a d+=1增加一天。

那么,这一行:

 xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 || goto loop 

检查由set /a d+=1部分组成的日期实际上是否存在于日历中。 如果形成的日期不存在,它只是“跳过”日期,移动到循环的开始添加一天。 这样,不存在的日期就不能设置为明天的日期。

if %d% gtr 31 (除非实际上是今天的 31天,否则没有任何作用。

所以,尽管if %d% gtr 31 (看起来有点混乱的部分,但是这个代码在那些不到31天的月份里仍然运行良好。

要更好地理解这一点,请打开@echo on并跟踪日期值中的更改。

例如,如果我们使用:

 set d=30 set m=04 set y=2016 

输出是:

 ************************ * Today: 30.04.2016 * * Tomorrow: 01.05.2016 * ************************ 

另外,对于:

 set d=28 set m=02 set y=2015 

输出:

 ************************ * Today: 28.02.2015 * * Tomorrow: 01.03.2015 * ************************ 

这是我的解决方案,以确定明天的日期总是正确的。

 @echo off setlocal EnableExtensions if "%~1" NEQ "" ( rem This is for fast testing determining the date of tomorrow from any rem date specified as parameter in format YYYYMMDD on calling this batch rem file from within a command prompt window. The parameter string is rem not validated at all as this is just for testing the code below. set "LocalDateTime=%~1" ) else ( rem Get local date and time in a region independent format. for /F "skip=1 tokens=1 delims=." %%D in ('%SystemRoot%\System32\wbem\wmic.exe OS get LocalDateTime') do set "LocalDateTime=%%D" & goto GetDate ) rem Get day, month and year from the local date/time string (or parameter). :GetDate set "Day=%LocalDateTime:~6,2%" set "Month=%LocalDateTime:~4,2%" set "Year=%LocalDateTime:~0,4%" rem Define a variable with today date in format DD.MM.YYYY set "Today=%Day%.%Month%.%Year%" rem Increase the day in month by 1 in any case. set /A Day+=1 rem The tomorrow date is already valid if the day of month is less than 29. if %Day% LSS 29 goto BuildTomorrow rem Tomorrow is next month if day is equal (or greater) 32. if %Day% GEQ 32 goto NextMonth rem Day 31 in month is not possible in April, June, September and November. rem In February it can't occur that day in month increased from 30 to 31 rem except on calling this batch file with invalid date string 20160230. if %Day% EQU 31 ( if "%Month%" == "04" goto NextMonth if "%Month%" == "06" goto NextMonth if "%Month%" == "09" goto NextMonth if "%Month%" == "11" goto NextMonth ) rem The day 29 and 30 in month is valid for all months except February. if NOT "%Month%" == "02" goto BuildTomorrow rem Determine if this year is a leap year with 29 days in February. set /A LeapYearRule1=Year %% 400 set /A LeapYearRule2=Year %% 100 set /A LeapYearRule3=Year %% 4 rem The current year is always a leap year if it can be divided by 400 rem with 0 left over (1600, 2000, 2400, ...). Otherwise if the current rem year can be divided by 100 with 0 left over, the current year is NOT rem a leap year (1900, 2100, 2200, 2300, 2500, ...). Otherwise the current rem year is a leap year if the year can be divided by 4 with 0 left over. rem Well, for the year range 1901 to 2099 just leap year rule 3 would be rem enough and just last IF condition would be enough for this year range. set "LastFebruaryDay=28" if "%LeapYearRule1%" == "0" ( set "LastFebruaryDay=29" ) else if NOT "%LeapYearRule2%" == "0" ( if "%LeapYearRule3%" == "0" ( set "LastFebruaryDay=29" ) ) if %Day% LEQ %LastFebruaryDay% goto BuildTomorrow rem Tomorrow is next month. Therefore set day in month to 1, increase the rem month by 1 and if now greater than 12, set month to 1 and increase year. :NextMonth set "Day=1" set /A Month+=1 if %Month% GTR 12 ( set "Month=1" set /A Year+=1 ) rem The leading 0 on month and day in month could be removed and so both rem values are defined again as string with a leading 0 added and next just rem last 2 characters are kept to get day and month always with 2 digits. :BuildTomorrow set "Day=0%Day%" set "Day=%Day:~-2%" set "Month=0%Month%" set "Month=%Month:~-2%" rem Define a variable with tomorrow date in format DD.MM.YYYY set "Tomorrow=%Day%.%Month%.%Year%" echo Today is: %Today% echo Tomorrow is: %Tomorrow% endlocal 

为了理解使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • wmic OS get /?