【问题标题】:Currentdate and Nearest date当前日期和最近日期
【发布时间】:2013-09-11 08:51:06
【问题描述】:

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

例如一个名为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) *

"

我该怎么做?

【问题讨论】:

  • 你有powershell吗?
  • 不,我不这么认为。我只有批次可用。
  • 您使用的是什么操作系统?我认为 Powershell 在 Vista 和更高版本中。下一个日期...总是文件的最后一行吗?
  • 你好 Foxidrive。我目前正在运行 Windows 7,但由于它是学校计算机,我无法访问所有内容。
  • 我有 powershell,但我更喜欢批量 :)(稍后也会转换为 .exe)。

标签: date datetime batch-file


【解决方案1】:

与:

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

【讨论】:

  • 结果:回显:“99999999”
  • 它无法找到 test2.txt ,即使我将它放在正确的路径中。
  • 你使用什么文件路径?有引号吗?
  • atm 文件与我的“脚本”位于同一文件夹中。 (文件)
  • 你能不能把它“转换”成我要求的主线程,(就像输出一样)
【解决方案2】:

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

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%.

倒计时:

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

使用 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)

DateDiff.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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2021-07-27
  • 1970-01-01
  • 2020-06-30
  • 2019-09-15
相关资源
最近更新 更多