如何编写批处理脚本,搜索此格式的Dates.txt文件:
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
但我无法弄清楚如何找到明天的日期,将其与档案中的日期进行比较。
一些帮助将不胜感激!
答案 0 :(得分:4)
好吧,我终于想到了自己!
@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 文件,该文件使用以下格式的日期:
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 st 当天今天。
所以,尽管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 *
************************
答案 1 :(得分:1)
以下是我确定明天的日期始终正确的解决方案。
@echo off
setlocal EnableExtensions
if "%~1" == "" (
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
) else (
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"
)
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.
rem It is necessary to remove leading 0 for the days 08 and 09 as
rem those two days would be otherwise interpreted as invalid octal
rem numbers and increment result would be 1 instead of 9 and 10.
rem if "%Day:~0,1%" == "0" set "Day=%Day:~1%"
rem set /A Day+=1
rem Faster is concatenating character 1 with the day string to string
rem representing 101 to 131 and subtract 99 to increment day by one.
set /A Day=1%Day%-99
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%Month%-99
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 /?