第二天的Windows批处理事件提醒 [英] Windows batch event reminder for next day
问题描述
如何编写批处理脚本,该脚本将在以下格式的Dates.txt文件中进行搜索:
How do I write a batch script, that would search in Dates.txt file of this format:
EventName1:dd.mm.yyyy
EventName2:dd.mm.yyyy
...
EventNameN:dd.mm.yyyy
对于具有明天日期的事件,如果发现,将其通知给用户吗?
for events with tomorrow's date, and if found, notify the user about them?
我能够为今天的活动编写脚本:
I was able to write a script for today's events:
@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
但是我不知道如何找到明天的日期并将其与文件中的日期进行比较.
But I can't figure out how to find tomorrow's date to compare it with the dates in file.
一些帮助将不胜感激!
推荐答案
好吧,我终于自己弄明白了!
Well, I have finally figured it myself!
@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%d%-99
if %d% gtr 31 (
set d=1
set /a m=1%m%-99
if %m% gtr 12 (
set m=1
set /a y+=1
)
)
xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 || goto loop
set td=0%d%
set td=%td:~-2%
set tm=0%m%
set tm=%tm:~-2%
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 文件,该文件使用以下格式的日期:
It works for the Dates.txt file, that uses dates in this format:
EventName1:2016年5月31日
EventName2:2016年5月3日
EventName3:2016年5月31日
EventName4:2016年6月1日
EventName5:2016年5月31日
EventName6:02.06.2016
EventName7:2016年6月1日
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的天和月中的前导零).
(Shouldn't forget about single empty spaces before and after colon, and about leading zeros for days and months that are less than 10.)
更新:
首先,set /a d+=1
增加一天.
然后,此行:
xcopy /d:%m%-%d%-%y% /l . .. >nul 2>&1 || goto loop
检查由set /a d+=1
部分组成的日期是否确实存在于日历中.如果形成的日期不存在,则只是跳过"日期,移至循环的开头再增加一天.这样,无法将不存在的日期设置为明天的日期.
checks if the date that was formed by set /a d+=1
part, actually exists in the calendar. If the date that was formed doesn't exist, it just "skips" the date, moving to the beginning of the loop to add one more day. This way, the date that doesn't exist can't be set as tomorrow's date.
if %d% gtr 31 (
部分不执行任何操作,除非它实际上是当日今天的月.
The if %d% gtr 31 (
part is not doing anything unless it is actually 31st day of month today.
因此,尽管if %d% gtr 31 (
部分看起来有些令人困惑,但是该代码在其中少于31天的月份中仍然可以很好地工作.
So, despite the if %d% gtr 31 (
part that looks somewhat confusing, this code still works well for months that have less than 31 days in them.
要更好地理解所有内容,请转动@echo on
并跟踪日期值的变化.
To understand it all better, turn @echo on
and trace the changes in the date values.
例如,如果我们使用:
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 *
************************
这篇关于第二天的Windows批处理事件提醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!