Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

date - Windows batch event reminder for next day

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.

Some help would be appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

It works for the Dates.txt file, that uses dates in this format:

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

(Shouldn't forget about single empty spaces before and after colon, and about leading zeros for days and months that are less than 10.)


UPDATE:

At first, set /a d+=1 adds a day.

Then, this line:

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

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.

The if %d% gtr 31 ( part is not doing anything unless it is actually 31st day of month today.

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.

To understand it all better, turn @echo on and trace the changes in the date values.

For example, if we use:

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

Output is:

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

Also, for:

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

Output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...