2

如何制作一个从文本文件中回显最近日期的批处理文件?

例如一个名为 test2.txt 的测试文件,在 text2.text 中你可以看到:

11.12.2013 historie (kapittel1, kapittel 2 og kapittel 3).
21.12.2013 matte (kapittelq1, kapittel 2 og kapiwettel 3).
01.12.2013 naturfag (kapittel1q, kapittel 2 og kapiwettel 3).
16.12.2013 example (kapittelq1, kapittel 2 og kapittelwe 3).
12.10.2013 example 2(kapittel1w, kapittel 2 og kapittweel 3).

然后它会在分析文件后回显,并回显最近的日期(从当前日期开始),如下所示:“

The Next test is X days (12.10.2013) and the subject is (example2, and the topics is (kapittel1w, kapittel 2 og kapittweel 3).
this means it is: X hours, X minutes, X secounds *( countdown) *

"

我该怎么做呢?

4

2 回答 2

0

和 :

11.12.2013 historie (kapittel1, kapittel 2 og kapittel 3).
21.12.2013 matte (kapittelq1, kapittel 2 og kapiwettel 3).
01.12.2013 naturfag (kapittel1q, kapittel 2 og kapiwettel 3).
16.12.2013 example (kapittelq1, kapittel 2 og kapittelwe 3).
12.10.2013 example 2(kapittel1w, kapittel 2 og kapittweel 3).

结果是20131012

@echo off &setlocal EnableDelayedExpansion

set "file_path=.\test2testtesttest.TXT"

if not exist "!file_path!" echo File "!file_path!" Does Not Exist >&2 & exit /b 2
for /f %%F in ("!file_path!") do  set file_path=%%~sfF


copy nul "%TEMP%\~.ddf" >nul

:: date function provided by carlos
:: http://www.dostips.com/forum/viewtopic.php?f=3&t=4555&start=15
makecab /D RptFileName="%TEMP%\~.rpt" /D InfFileName="%TEMP%\~.inf" -f "%TEMP%\~.ddf">nul
for /f "tokens=3-7" %%a in ('type "%TEMP%\~.rpt"') do (
   if not defined current-date set "current-date=%%e-%%b-%%c"
   if not defined current-time set "current-time=%%d"
   if not defined weekday set "weekday=%%a"
)
del /q "%TEMP%\~.*"
rem echo %weekday% %current-date% %current-time%
set Jan=01
set Feb=02
set Mar=03
set Apr=04
set May=05
set Jun=06
set Jul=07
set Aug=08
set Sep=09
Set Oct=10
set Nov=11
set Dec=12

for /F "tokens=1,2,3 delims=-" %%A in ("%current-date%") do (
    set comparable_date=%%A!%%B!%%C
)
rem echo %comparable_date%
set /a nearest_date=99999999
for /F "tokens=1,2,3 delims=. " %%A in (%file_path%) do (

    set /a possible_nearest_date=%%C%%B%%A

    set /a old_result=!nearest_date!-comparable_date
    set /a new_result=!possible_nearest_date!-comparable_date

    if !new_result! LSS !old_result! set /a nearest_date=!possible_nearest_date!
)
if !nearest_date! EQU 99999999 echo something wrong with the file>&2 && endlocal && exit /b 3

set nearest_date=!nearest_date:~-2!.!nearest_date:~4,2!.!nearest_date:~0,4!

echo(
echo(
rem type %file_path%
for /f "tokens=1,2* delims=()" %%L in ('type %file_path%^|find "!nearest_date!"') do ( 

    for /f "tokens=1,2" %%t in  ("%%L") do (
        echo The Next Test is on (!nearest_date!^) and subject is ( %%u   with topics   %%M^)
    )   
)
echo(
echo(
endlocal
于 2013-09-11T09:43:18.297 回答
0

精简获取下一个事件部分:

FOR /F "TOKENS=1,* DELIMS==" %%c IN ('WMIC OS GET /FORMAT:VALUE') DO IF /I "%%c" == "LocalDateTime" SET CurrentDate=%%d
SET CurrentDate=%CurrentDate:~0,8%

SET NextDate=99999999
FOR /F "TOKENS=1,2,3,* DELIMS=. " %%i IN (text2.txt) DO IF %%k%%j%%i GTR %CurrentDate% FOR /F %%r IN ('SET /A NextDate-=%%k%%j%%i') DO IF %%r GTR 0 (
    SET NextDate=%%k%%j%%i
    SET NextSubject=%%l
)

ECHO The Next test is (%NextDate:~6,2%.%NextDate:~4,2%.%NextDate:~0,4%) and the subject is %NextSubject%.

倒计时器:

这是一个困难的部分。我已经批量完成了日期逻辑(如这里),但它非常耗时。

使用 VBScripting 替代或创建一个小程序来完成这项工作。

编辑:@npocmaka 注意到WMIC并非所有 Windows 版本的标准,我正在使用 VBS 为这两个部分添加解决方案。

批处理文件:

FOR /F "TOKENS=*" %%d IN ('CSCRIPT /NOLOGO Now.vbs') DO SET CurrentDate=%%d
SET NextDate=99999999
FOR /F "TOKENS=1,2,3,* DELIMS=. " %%i IN (text2.txt) DO IF "%%k %%j %%i" GTR "%CurrentDate%" FOR /F %%r IN ('SET /A NextDate-=%%k%%j%%i') DO IF %%r GTR 0 (
    SET NextDate=%%k%%j%%i
    SET "NextSubject=%%l"
)
FOR /F "TOKENS=1,2,3,4" %%d IN ('CSCRIPT /NOLOGO DateDiff.vbs %NextDate:~0,4% %NextDate:~4,2% %NextDate:~6,2% 00 00 00 %CurrentDate%') DO ECHO The Next test is %%d days (%NextDate:~6,2%.%NextDate:~4,2%.%NextDate:~0,4%) and the subject is %NextSubject%.
FOR /F "TOKENS=1,2,3,4" %%d IN ('CSCRIPT /NOLOGO DateDiff.vbs %NextDate:~0,4% %NextDate:~4,2% %NextDate:~6,2% 00 00 00 %CurrentDate%') DO ECHO this means it is: %%e hours, %%f minutes, %%g seconds

现在.vbs:

d=Now
WScript.Echo Right("000"&Year(d),4)&" "&Right("0"&Month(d),2)&" "&Right("0"&Day(d),2)&" "&Right("0"&Hour(d),2)&" "&Right("0"&Minute(d),2)&" "&Right("0"&Second(d),2)

日期差异.vbs:

If WScript.Arguments.Count <> 12 Then
    WScript.Echo "Syntax: DAYSDIFF yyyy mm dd hh mm ss yyyy mm dd hh mm ss"
Else
    d=DateSerial(WScript.Arguments(0),WScript.Arguments(1),WScript.Arguments(2))+TimeSerial(WScript.Arguments(3),WScript.Arguments(4),WScript.Arguments(5))-DateSerial(WScript.Arguments(6),WScript.Arguments(7),WScript.Arguments(8))-TimeSerial(WScript.Arguments(9),WScript.Arguments(10),WScript.Arguments(11))
    WScript.Echo Cdbl(Fix(d))&" "&Right("0"&Hour(d),2)&" "&Right("0"&Minute(d),2)&" "&Right("0"&Second(d),2)
End IF
于 2013-09-11T13:02:26.473 回答