在批处理文件中查找文件的修改日期是否早于N小时,如果不是,则发送电子邮件 [英] Find out if file's modified date is older than N hours in Batch file and send email if not

查看:120
本文介绍了在批处理文件中查找文件的修改日期是否早于N小时,如果不是,则发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这篇文章正是我所期待的:



然而,它似乎并没有工作。任何人都可以帮忙吗?

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

解决方案

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

  @ECHO OFF& SETLOCAL 
::初始化
CALL:GetInternational

::示例
SETdate1 = Fr 08/30/2013
SETtime1 = 19:50:52,17

:获取秒
调用:GetSecs%date1%%time1 %sec1
call GetSecs%date%%time%sec2

::计算分钟
set / a elapsedMin =(sec2-sec1)/ 60
回声此答案是%elapsedMin%分钟(S)旧(CET)。

::计算小时
::请注意,cmd不能计算十进制数字,例如: 1.23
set / a elapsedHr =(sec2-sec1)/ 3600
回答此答案是%elapsedHr%小时(CET)。

:: goto文件结尾
goto:eof

:GetInternational
::通过读取注册表设置
'for / ftokens = 1,2 *%% a in('reg queryHKCU\Control Panel\International')do set%% a = %% c
goto:eof
$ b $ GetSecsdateIntimeInsecondsOut
:: Output:自1970年1月1日以来的秒数00:00:00
setlocal
setdateIn = %〜1
for / ftokens = 2%% i in(%dateIn%)do setdateIn = %% i
for / ftokens = 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 / ftokens = 1-3 delims =%sTime %% sDecimal%%% in(%〜2)do(
sethh = %% a
setnn = %% b
setss = %% c

if 1%hh%lss 20 set hh = 0%hh%
如果%nn:〜2,1%等于p如果%hh%neq1 2(设置hh = 1%hh%和设置/ hh- = 88)
如果%nn:〜2,1%equaif%hh%equ12 设置hh = 00
如果%nn:〜2,1%geqa设置nn =%nn:〜0.2%
set / a hh = 100% hh %%% 100,nn = 100%nn %%% 100,ss = 100%ss %%% 100
set / az = 14-mm,z / = 12,y = yy + 4800-z, M =毫米+ 12 * Z-3,J = 153 * M + 2,当J = J / 5 + DD + Y * 365 + Y / 4-γ/ 100 + Y / 400-2472633,当J = J * 86400 + hh * 3600 + nn * 60 + ss
endlocal& set%〜3 =%j%
goto:eof

资料来源:

I thought this post was exactly what I was looking for:

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

However, it doesn't seem to work. Can anyone help?

I have an aplication running on many servers that crashes every once and a while. When it crashes, it no longer writes to its own log file. Therefor, if I can write a bat file to check if that log file has not been modified recently, it can email me an alert. Unfortunately, I need to know if this file has been changed within the last 4 to 5 hours. Knowing if the file was modified within the last day is not good enough. Thanks!

解决方案

try this, explanation is in the code:

@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

Source: dostips.com

To get the file time and send the email I leave for the OP.

这篇关于在批处理文件中查找文件的修改日期是否早于N小时,如果不是,则发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆