批处理脚本来检查目录中最新的文件创建时/修改 [英] Batch script to check when the newest file in a directory was created/modified

查看:337
本文介绍了批处理脚本来检查目录中最新的文件创建时/修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新手编剧所以请原谅我。我需要创建一个Windows批处理脚本,完成以下操作:

I'm quite the novice scripter so please forgive me. I need to create a windows batch script that accomplishes the following:


  • 检查指定目录的根目录和识别最近修改或创建文件

  • 如果该文件没有在过去的24小时内修改或创建(该批处理文件运行时从),然后执行另一个指定的脚本。我已经有这个脚本来处理自己触发警报/电子邮件。

我将通过任务调度程序来运行此。任何帮助将大大AP preciated。谢谢!

I will be running this via task scheduler. Any help would be greatly appreciated. Thank you!

推荐答案

这命令列出了修改时间排序的文件。最新的一个是最后一个。

This command lists the files ordered by modification time. The latest one is the last one.

> dir /Od /B

此命令运行previous之一,并存储在变量文件的最后一个文件

This command runs the previous one and store the last file in variable FILE

> for /F "usebackq" %f in (`dir /Od /B`) do set "FILE=%f"

此命令显示文件信息的特定文件:

This command shows file information for a specific file:

> dir /N "%FILE%" | findstr /B "[0-9]"

此命令会在previous命令,并解析它。字段(年,月,日...)和分隔符的顺序(/日期,:在我的情况小时)。是依赖于区域设置

This command will run the previous command, and parse it. The order of fields (year, month, day...) and the delimiters (/ for date, : for hour in my case) is dependent on your regional settings.

> for /f "usebackq tokens=1-5 eol=\  delims=/:\ " %i in (`dir /N "%FILE%" ^| findstr /B "[0-9]"`) do echo [%i%j%k] [%l%m]

我们可以将结果存储在变量:

We can store the result in variables:

> for /f "usebackq tokens=1-5 eol=\  delims=/:\ " %i in (`dir /N "%FILE%" ^| findstr /B "[0-9]"`) do set DD=%i&set MM=%j&set YYYY=%k&set HH=%l&set MI=%m

的那些命令显示当前日期和时间:

Those commands show the current date and time:

> date /t
> time /t

运行这些命令,解析并存储到变量:

Those commands run, parse and stores into variables:

> for /F "usebackq tokens=1-3 delims=%DATE_DELIM%" %i in (`date /t`) do set DD=%i&set MM=%j&set YYYY=%k
> for /F "usebackq tokens=1,2 delims=%TIME_DELIM%" %i in (`time /t`) do set HH=%i&set MI=%j

这是比较容易的部分。现在,我们必须做一些算术比较的日期。 设置/ A 可以做算术,但会间preT08为八进制。因此,我们必须通过消除值转换为十进制领先的0:

That was the easy part. We now have to do some arithmetics to compare the dates. set /A can do arithmetics, but will interpret "08" as octal. So we have to convert values to decimal by removing the leading '0':

> set /A DD=1%DD%-100&set /A MM=1%MM%-100
> set /A HH=1%HH%-100&set /A MI=1%MI%-100

下面是最终的脚本。
我不得不用很多花样做日期/时间算术等24小时难codeD。

Here is the final script. I had to use a lot of tricks to do date/time arithmetics, and so "24h" is hard coded.

@echo off
setlocal
set DIR=%~f1
set DATE_DELIM=/
set TIME_DELIM=:
for /F "usebackq" %%f in (`dir /Od /B "%DIR%"`) do set "FILE=%%f"

for /F "usebackq tokens=1-5 eol=\  delims=%DATE_DELIM%%TIME_DELIM%\ " %%i in (`dir /N "%FILE%" ^| findstr /B "[0-9]"`) do set A_DD=%%i&set A_MM=%%j&set A_YYYY=%%k&set A_HH=%%l&set A_MI=%%m
set A_DATE=%A_YYYY%%A_MM%%A_DD%
set /A A_DD=1%A_DD%-100&set /A A_MM=1%A_MM%-100
set /A A_TIME=(1%A_HH%-100)*60+%A_MI%-100

for /F "usebackq tokens=1-3 delims=%DATE_DELIM%\ " %%i in (`date /t`) do set B_DD=%%i&set B_MM=%%j&set B_YYYY=%%k
for /F "usebackq tokens=1,2 delims=%TIME_DELIM%\ " %%i in (`time /t`) do set B_HH=%%i&set B_MI=%%j
set B_DATE=%B_YYYY%%B_MM%%B_DD%
set /A B_DD=1%B_DD%-100&set /A B_MM=1%B_MM%-100
set /A B_TIME=(1%B_HH%-100)*60+%B_MI%-100

echo %A_DATE% %A_TIME% - %A_YYYY% %A_MM% %A_DD%
echo %B_DATE% %B_TIME% - %B_YYYY% %B_MM% %B_DD%

:: Check that the file is newer than 24 hours
:: This will not work at daylight change
:: This is probably full of bugs as I had no time to test all cases

if %A_DATE% GEQ %B_DATE% goto :NewFile

:: Past day
:: Whatever the day, if the time of the day is <, this is old
if %A_HH%%A_MI% LSS %B_HH%%B_MI% goto :OldFile

if not %B_MM%==1 goto :WhateverMonth

if %A_DATE:~1,6% EQU %B_DATE:~1,6% goto :SameMonth
:: Change Month
set /A D=%B_YYYY%*12+%B_MM%-%A_YYYY%*12+%A_MM%
if not %D%==-1 goto :OldFile
:LastMonth
:: Is A the last day of the month => yesterday ?
if %A_DD% EQU 2 goto :February
:: 31 is of course the last day
if %A_DD% EQU 31 goto :NewFile
if %A_DD% LSS 30 goto :OldFile

:: Months of 30 days
if %A_DD%==4 goto :NewFile
if %A_DD%==6 goto :NewFile
if %A_DD%==9 goto :NewFile
if %A_DD%==11 goto :NewFile
:: Day is 30 and month has 31 days
goto :OldFile

:February
set /A D=28+!(%A_YYYY% ^% 4)
if %A_DD%==%D% goto :NewFile
:: We do not have to handle others bissextile case which will happen in about
:: 389 years
goto :OldFile

:WhateverMonth
if %A_YYYY%%A_MM% EQU %B_YYYY%%B_MM% goto :SameMonth
goto :OldFile

:SameMonth
set /A D=100+%B_DD%-%A_DD%
if %D% GTR 101 goto :OldFile
:Yesterday
if %B_DD% GTR 1 goto :ChkTime

:ChkTime

:OldFile
echo %FILE% is old.
goto :End

:NewFile
echo %FILE% is new.
goto :End

:End
endlocal

要运行它:

> CheckFile.cmd <directory>

这只是告诉你,这是的可能。但我不会建议把它在生产前广泛的测试。而且它的难以维持。所以不是批我建议在WSH环境或PowerShell来使用JScript的。

This was just to show you that it is possible. But I would not recommend to put it in production before extensive testing. And it is hard to maintain. So instead of batch I recommend to use JScript in the WSH environment or PowerShell.

这篇关于批处理脚本来检查目录中最新的文件创建时/修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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