【问题标题】:Find out if file's modified date is older than N hours in Batch file and send email if not找出文件的修改日期是否早于批处理文件中的 N 小时,如果不是则发送电子邮件
【发布时间】:2013-08-30 15:35:05
【问题描述】:

我认为这篇文章正是我想要的:

Find out if file is older than 4 hours in Batch file

但是,它似乎不起作用。有人可以帮忙吗?

我有一个运行在许多服务器上的应用程序,每隔一段时间就会崩溃。当它崩溃时,它不再写入自己的日志文件。因此,如果我可以编写一个 bat 文件来检查该日志文件最近是否未被修改,它可以通过电子邮件向我发送警报。不幸的是,我需要知道这个文件是否在过去 4 到 5 小时内被更改过。知道文件是否在最后一天被修改是不够的。谢谢!

【问题讨论】:

    标签: file batch-file


    【解决方案1】:

    试试这个,解释在代码中:

    @ECHO OFF &SETLOCAL
    :: Initialisation
    CALL :GetInternational
    
    :: Example
    SET "date1=Fr 08/30/2013"
    SET "time1=19:50:52,17"
    
    :: Get the seconds
    call :GetSecs "%date1%" "%time1%" sec1
    call :GetSecs "%date%" "%time%" sec2
    
    :: calculate the minutes
    set /a elapsedMin="(sec2-sec1)/60"
    echo This answer is %elapsedMin% minute(s) old (CET).
    
    :: calculate the hours
    :: please note, cmd can't calculate decimal numbers, e.g. 1.23
    set /a elapsedHr="(sec2-sec1)/3600"
    echo This answer is %elapsedHr% hour(s) old (CET).
    
    :: goto end of file
    goto:eof
    
    :GetInternational
    :: Sets a bundle of variables by reading the registry settings
    for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"') do set "%%a=%%c"
    goto :eof
    
    :GetSecs "dateIn" "timeIn" secondsOut
    ::  Output:  Seconds elapsed since 1th Jan. 1970 00:00:00
    setlocal
    set "dateIn=%~1"
    for /f "tokens=2" %%i in ("%dateIn%") do set "dateIn=%%i"
    for /f "tokens=1-3 delims=%sDate%" %%a in ("%dateIn%") do (
      if %iDate%==0 set /a mm=100%%a%%100,dd=100%%b%%100,yy=10000%%c%%10000
      if %iDate%==1 set /a dd=100%%a%%100,mm=100%%b%%100,yy=10000%%c%%10000
      if %iDate%==2 set /a yy=10000%%a%%10000,mm=100%%b%%100,dd=100%%c%%100
    )
    for /f "tokens=1-3 delims=%sTime%%sDecimal% " %%a in ("%~2") do (
      set "hh=%%a"
      set "nn=%%b"
      set "ss=%%c"
    )
    if 1%hh% lss 20 set hh=0%hh%
    if "%nn:~2,1%" equ "p" if "%hh%" neq "12" (set "hh=1%hh%" &set /a hh-=88)
    if "%nn:~2,1%" equ "a" if "%hh%" equ "12" set "hh=00"
    if "%nn:~2,1%" geq "a" set "nn=%nn:~0,2%"
    set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
    set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2,j=j/5+dd+y*365+y/4-y/100+y/400-2472633,j=j*86400+hh*3600+nn*60+ss
    endlocal &set "%~3=%j%"
    goto :eof
    

    来源:dostips.com

    要获取文件时间并发送我留给 OP 的电子邮件。

    【讨论】:

    • 计算时间戳的令人印象深刻的公式,它就像一个魅力+1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 2014-11-27
    相关资源
    最近更新 更多