文件已在过去15分钟内修改过--Batch Script

时间:2016-08-10 08:44:39

标签: date batch-file date-difference

嗨,我需要帮助,因为我是BatchScript的新手。 我需要检查目录中的任何文件/文件夹是否在最近15分钟内被修改。

这是我的逻辑:

  1. 查找上次修改日期
  2. 查找当前日期
  3. 查看这两者之间的差异是否为15分钟。 我能够完成前两个步骤。我坚持第三个步骤 请帮我找出这两个日期之间的时差。 或者,如果有更好/更容易的逻辑。
  4. 这是我的代码:

    @echo off
    for /f %a in (' dir "D:\BatchFiles" /od/b/s/t') do set Date1= %~ta
    echo The most recently created file is %Date1%
    @echo off
    for /f "delims=" %i in ('time /t') do set output=%i
    @echo off
    SET Date2= %DATE:~4,2%/%DATE:~7,2%/%DATE:~10,4% %output%
    echo The current date is %Date2%
    PAUSE
    

1 个答案:

答案 0 :(得分:0)

这是一个纯批处理文件,它返回给定年龄的给定目录中的所有文件,但有以下限制:

  • 日期未正确解析,因此如果在上个月修改了某个文件,则可能不会错误地将其包含在结果中;如果要以天数来定义最大年龄,forfiles solution可能更合理;
  • 在路径中同时包含字符),的文件将在输出中丢失;这是因为design flaw of the wmic command,用于检索上次修改文件的与语言环境无关的日期/时间信息;

要使用脚本 - 让我们称之为max-aged-files.bat - ,提供如下命令行参数:

max-aged-files.bat 15*60 "D:\BatchFiles"

第一个参数是文件的最大年龄,以秒为单位;像15*60这样的简单算术表达式被理解。第二个参数是用于搜索文件的位置和/或文件模式;您可以在此处声明"D:\BatchFiles"之类的目录路径,或"*.bat"之类的文件模式,或"D:\BatchFiles\*.bat"之类的组合;你省略它,使用当前目录。

以下是代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem /* Please regard that this script cannot handle dates correctly!
rem    so if current date and file date are in different months it fails! */

rem // Retrieve and prepare arguments:
set "MAXAGE=%~1" & rem // (maximum age of files in terms of seconds)
if defined MAXAGE (set /A "MAXAGE=%MAXAGE%+0") else (
    >&2 echo ERROR: maximum age not specified! & exit /B 1
)
if %MAXAGE% GEQ 86400 (>&2 echo ERROR: maximum age exceeds range! & exit /B 1)
shift /1
set "LOCATION=%~1"
set "ATTR=%~a1"
set "ATTR=%ATTR:~,1%"
if not defined LOCATION (set "LOCATION=.\*.*") else (
    if "%ATTR%"=="d" set "LOCATION=%LOCATION%\*.*"
)
rem /* Gather current date/time in standardised format
rem    [like: `YYYYMMDDHHMMSS.UUUUUU+ZZZ`]: */
for /F "delims=" %%I in ('wmic OS GET LocalDateTime') do (
    for /F "delims=" %%J in ("%%I") do set "CURRTIME=%%J"
)
rem // Extract `YYYYMMSS` portion from current date/time:
set "CURRDATE=%CURRTIME:~,8%"
rem // Extract `HHMMSS` portion from current date/time only:
for /F "delims=." %%T in ("%CURRTIME:~8%") do (
    set "CURRTIME=%%T"
)
rem // Loop through all files at given location:
for %%F in ("%LOCATION%") do (
    set "ITEM=%%~fF"
    setlocal EnableDelayedExpansion
    rem // Gather file date/time in standardised format:
    for /F "delims=" %%E in ('
        2^> nul wmic DataFile WHERE Name^="!ITEM:\=\\!" GET LastModified ^|^| ^
        2^> nul wmic DataFile WHERE ^(Name^="!ITEM:\=\\!"^) GET LastModified
    ') do (
        for /F "delims=" %%F in ("%%E") do set "FILETIME=%%F"
    )
    rem // Extract `YYYYMMSS` portion from file date/time:
    set "FILEDATE=!FILETIME:~,8!"
    rem // Extract `HHMMSS` portion from file date/time:
    for /F "delims=." %%T in ("!FILETIME:~8!") do (
        set "FILETIME=%%T"
    )
    rem // Compute date difference between file and current date:
    set /A "DATEDIFF=CURRDATE-FILEDATE"
    rem // Continue processing only if date difference is zero or one:
    if !DATEDIFF! GEQ 0 if !DATEDIFF! LEQ 1 (
        rem // Convert date difference to seconds:
        set /A "DATEDIFF*=240000"
        rem // Compute time difference, regarding also date difference:
        set /A "TIMEDIFF=DATEDIFF+1!CURRTIME!-1!FILETIME!"
        rem // Pad time difference to consist of 6 digits [like `HHMMSS`]:
        set "TIMEDIFF=000000!TIMEDIFF!" & set "TIMEDIFF=!TIMEDIFF:~-6!"
        rem // Convert time difference to seconds:
        set /A "TIMEDIFF=1!TIMEDIFF:~-2!-100+60*(1!TIMEDIFF:~-4,-2!-100+60*(1!TIMEDIFF:~-6,-4!-100))"
        rem // Return item if 
        if !TIMEDIFF! LEQ %MAXAGE% (
            echo(!ITEM!
        )
    )
    endlocal
)

endlocal
exit /B