查找出来,如果​​文件是批处理文件老年超过4小时 [英] Find out if file is older than 4 hours in Batch file

查看:199
本文介绍了查找出来,如果​​文件是批处理文件老年超过4小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单,我怎么会发现,如果一个文件夹中的特定文件比X小时旧的?
文件的名称和位置将始终是相同的。

Quite simply, how would I find out if a specific file in a folder is older than X hours? The file name and location will always be the same.

感谢

推荐答案

在有批次code以下比较文件的时间必须考虑到:

On comparing file times with batch code following must be taken into account:


  1. 日期和时间格式设置

  2. 存储介质的文件系统

  3. 时区,夏令时间

  4. 自动调整为夏令时

  5. 的Windows版本

有环境变量的日期时间其中上访问(而不是批处理执行的开始)返回的电流根据的Windows区域和语言日期和时间格式设置。国家规定了日期和时间格式。


1. Date and time format settings

There are the environment variables DATE and TIME which on access (and not on start of batch execution) return current date and time in a format depending on Windows Region and Language settings. The country defines the date and time format.

甚至有可能出现与 DD.MM.YYYY 定义,例如德国短日期格式(日,月与领先的零)和时间格式与 HH:MM:SS (24小时格式与领先的零小时,分钟和秒)。但是,这并不意味着日期时间真正使用这种格式的该值。例如时间是格式为: H:MM:SS,MS 当已经选择了德国国家甚至与 HH:MM :SS 定义,这意味着对时辰,没有前导零,但在分秒,并及时串一个逗号后面还毫秒。日期字符串可也有或没有平日依赖于区域设置。

It is even possible there to define for example for German short date format with DD.MM.YYYY (day and month with leading zero) and time format with HH:mm:ss (24 hour format with leading zero for hour, minute and second). But this does not mean that value of DATE and TIME are really using this format. For example TIME is in format H:mm:ss,ms when having selected a German country even with HH:mm:ss defined which means no leading zero on hour, but on minute and second, and additionally milliseconds after a comma in time string. The date string can be also with or without weekday depending on region settings.

和有图案%〜牛逼来得到一个目录或文件的最后修改日期和时间。在命令提示符窗口中运行呼叫/?为/?并阅读显示的这两个命令所有帮助页面细节。通过%〜牛逼返回的日期和时间字符串以及显示在命令输出格式的 DIR 也取决于在Windows区域设置,但通常不变量的日期时间相同的格式。通常返回的文件或目录的最后修改时间的时间无日期/时间字符串第二个值。

And there is the pattern %~t to get last modification date and time of a directory or file. Run in a command prompt window call /? and for /? and read all help pages displayed for those two commands for details. The format of the date and time string returned by %~t as well as displayed on output of command DIR depends also on Windows region settings, but is usually not equal the format of variables DATE and TIME. The time of last modification time of a file or directory is usually returned without second value in date/time string.

最好的是找出具有下列前10:00 AM什么当前机器上的格式,当前用户的最佳执行批处理文件。

Best is to find out with following batch file best executed before 10:00 AM what are the formats on current machine for current user.

@echo off
echo Current date/time: %DATE% %TIME%
for %%I in ("%~f0") do echo Batch file time:   %%~tI
pause


2。存储介质的文件系统

在Windows文件系统两种存储格式用于文件次的:


    使用
  • 在存储介质上的 NTFS 的的 NTFS precision时间使用这是自上午12:00之后所经过的100纳秒为间隔的64位数字1601年1月1日协调世界时(UTC)。

  • On storage media using NTFS the NTFS precision time is used which is a 64-bit number of 100-nanosecond intervals that have elapsed since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).

在使用FAT,FAT16,FAT32或exFAT的文件时间使用存储的 DOS日期时间格式有两个16位值2秒的分辨率,采用当前本地时间。

On storage media using FAT, FAT16, FAT32 or exFAT the file times are stored using DOS Date Time format with two 16-bit values with a resolution of 2 seconds and using current local time.

这意味着一个奇怪的第二个不可能在FAT媒体文件,但在NTFS媒体。复制与最后修改时间奇数第二到FAT32驱动器会导致NTFS驱动器上的文件具有与最后修改时间1秒差相同的文件。

This means an odd second is not possible for a file on FAT media, but on NTFS media. Copying a file on an NTFS drive with an odd second in last modification time to a FAT32 drive results in having identical files with 1 second difference in last modification time.

在NTFS媒体文件时间存储在UTC。那么,什么是由%〜牛逼退回或者命令的 DIR 以及什么是在Windows资源管理器中显示例如取决于

The file times on NTFS media are stored in UTC. So what is returned by %~t or command DIR as well as what is displayed for example in Windows Explorer depends on


  • 当前时区,

  • 夏令时此时区,

  • 如果当前时间日光节约之内的时间段,

  • 如果夏令时自动调整当前启用。

更改其中任何4个参数的结果在所有的即时变化对NTFS存储介质显示文件和目录的文件时间。

Changing any of those 4 parameters results in an immediate change of ALL displayed file times of files and directories on NTFS storage media.

在FAT媒体文件的时间是作为使用本地时间创建,修改或上次访问或多或少不变,但对于细节进一步阅读。

File times on FAT media are more or less constant as using local time on creation, modification or last access, but read further for details.

由于在NTFS媒体文件时间存储在UTC,但在本地时间显示的文件做了NTFS和FAT32驱动器时的当前时区和夏令时此时区是重要的文件时比较,特别是

As file times on NTFS media are stored in UTC, but displayed in local time, the current time zone and the daylight saving time for this time zone are important for file time comparisons, especially when done for files on NTFS and FAT32 drives.

但也为FAT媒体,时区和夏令时设置的文件可以根据Windows版本是很重要的。阅读下面的详细信息。

But also for files on a FAT media, the time zone and daylight saving time settings could be important depending on version of Windows. Read below for details.

有是用于夏令时因为默认情况下使能和当前设置的时间段定义夏令时调整它变得很重要自动调节的设置。

There is the setting for automatic adjustment for daylight saving time which becomes important when being enabled as by default and currently set time zone defines a daylight saving time adjustment.

例如日光节约时间目前活跃在欧洲。 -1小时禁用Windows中导致在NTFS媒体所显示的文件时间立即改变时钟设置这个选项,同时还取决于Windows版本上的FAT许多媒体文件。

For example daylight saving time is active currently in Europe. Disabling this option in clock settings of Windows results in an immediate change of the displayed file times on NTFS media by -1 hour, and also many files on FAT media depending on version of Windows.

造成夏令时调整这条小时的时差,必须考虑到对NTFS和FAT媒体比较文件的时间。

This 1 hour time difference caused by the daylight saving time adjustment must be taken into account on comparing file times on NTFS and FAT media.

NTFS是由所有Windows NT版本的支持。文件时间的行为等于在所有的Windows版本支持NTFS文件和目录。

NTFS is supported by all Windows NT versions. File times behavior is equal for files and directories on all Windows versions supporting NTFS.

但在FAT文件媒体时代如何互为preTED取决于Windows版本。在此之前的Windows Vista在FAT媒体文件的时间是独立于时区的变化,夏令时间设定和夏令时自动调整。最后修改日期/文件的时间是恒定在Windows 2000和Windows XP。

But how file times on FAT media are interpreted depends on version of Windows. Prior Windows Vista the file times on FAT media are independent on change of time zone, daylight saving time setting and automatic adjustment of daylight saving time. Last modification date/time of a file is constant on Windows 2000 and Windows XP.

但在Windows Vista和后来FAT媒体所显示的文件时间取决于夏令时和夏令时的自动调整。时区不直接关系。选择作为目前用作它为NTFS上的媒体文件,不改变文件显示的文件次FAT媒体另一个时区。但是,如果当前时间是活动的时区和夏令时的自动调整夏令时间内启动和文件或目录的本地时间为夏令内节约的时间段,1小时是由Windows Vista中添加然后。在标准时间内FAT驱动器上一次修改的文件是过去一年不变;只是DST期间内最后一次修改的文件会显示带有或不依赖于当前的时间启用自动调整DST1小时。

But on Windows Vista and later the displayed file times on FAT media depend on daylight saving time and automatic adjustment for daylight saving time. The time zone does not directly matter. Selecting another time zone as currently used does not change the displayed file times of files on FAT media as it does for files on NTFS media. But if current time is within daylight saving time period of the active time zone and the automatic adjustment for daylight saving time is enabled and the local time of the file or directory is within the daylight saving time period, +1 hour is added by Windows Vista and later. The files last modified on FAT drives within standard time are constant over the year; just the files last modified within DST period are displayed with or without +1 hour depending on current time with enabled automatic DST adjustment.

这有例如,当一个文件夹的公共共享Windows 7机器上的FAT32驱动器上,并连接到这个公共文件夹在Windows XP的机器不同的FAT文件时间管理可以非常混乱。一个文件最后修改于2015年9月19日17时39分08秒存储FAT32驱动器在Windows上今天显示7 PC的Windows 7与德国时区与文件时间2015年9月19日17时39分08秒,但2个月,虽然与2015年9月19日16时39分08秒修改,和Windows XP连接的Windows 7 PC上的今天显示相同的文件,在文件时间2015年9月19日17时39分08秒。未来恒

That different FAT file time management can be extremely confusing when having for example a public share of a folder on a FAT32 drive on a Windows 7 machine and being connected to this public folder with a Windows XP machine. A file last modified on 2015-09-19 17:39:08 stored on FAT32 drive on Windows 7 PC is displayed today by Windows 7 with German time zone with file time 19.09.2015 17:39:08, but in 2 months although not modified with 19.09.2015 16:39:08, and Windows XP displays same file on connected Windows 7 PC today and in future constant with file time 19.09.2015 17:39:08.

比较与NTFS或FAT媒体可以是文件存储在归档(ZIP,RAR,...)文件的文件真的时代的噩梦。

Comparing file times of files stored in archives (ZIP, RAR, ...) with files on NTFS or FAT media can be really a nightmare.

对于这个任务,与当前时间的文件的最后修改时间进行比较,它应该是不够的,只是采取的日期和时间格式设置进去。

For this task comparing last modification time of a file with current time, it should be enough to just take date and time format settings into account.

下一批code使用code详见我的回答解释在批处理文件删除文件N多年长天。这种解释应该使用下面的code前阅读。

The batch code below uses code explained in detail in my answer on Batch file to delete files older than N days. This explanation should be read before using the code below.

根据日期/变量的日期时间格式为:时间按返回,日期/时间字符串%〜TI 本地的Windows PC上的文件,可能有必要进行小的修改,以code。适当的提示,给出批次code的注释行。

Depending on date/time format of the variables DATE and TIME and date/time string returned by %~tI for the file on local Windows PC, it might be necessary to make small modifications to code. Appropriate hints are given in the comment lines of batch code.

@echo off
setlocal EnableExtensions

rem Get seconds since 1970-01-01 for current date and time.
rem From date string only the last 10 characters are passed to GetSeconds
rem which results in passing dd/mm/yyyy or dd.mm.yyyy in expected format
rem to this subroutine independent on date string of environment variable
rem DATE is with or without abbreviated weekday at beginning.
call :GetSeconds "%DATE:~-10% %TIME%"

rem Subtract seconds for 4 hours (4 * 3600 seconds) from seconds value.
set /A "CompareTime=Seconds-4*3600"

rem Define batch file itself as the file to compare time by default.
set "FileToCompareTime=%~f0"

rem If batch file is started with a parameter and the parameter
rem specifies an existing file (or directory), compare with last
rem modification date of this file.
if not "%~1" == "" (
    if exist "%~1" set "FileToCompareTime=%~1"
)

rem Get seconds value for the specified file.
for %%F in ("%FileToCompareTime%") do call :GetSeconds "%%~tF:0"

rem Compare the two seconds values.
if %Seconds% LSS %CompareTime% (
    echo File %FileToCompareTime% was last modified for more than 4 hours.
) else (
    echo File %FileToCompareTime% was last modified within the last 4 hours.
)
endlocal
goto :EOF


rem No validation is made for best performance. So make sure that date
rem and hour in string is in a format supported by the code below like
rem MM/DD/YYYY hh:mm:ss or M/D/YYYY h:m:s for English US date/time.

:GetSeconds

rem If there is " AM" or " PM" in time string because of using 12 hour
rem time format, remove those 2 strings and in case of " PM" remember
rem that 12 hours must be added to the hour depending on hour value.
set "DateTime=%~1"
set "Add12Hours=0"
if "%DateTime: AM=%" NEQ "%DateTime%" (
    set "DateTime=%DateTime: AM=%"
) else if "%DateTime: PM=%" NEQ "%DateTime%" (
    set "DateTime=%DateTime: PM=%"
    set "Add12Hours=1"
)

rem Get year, month, day, hour, minute and second from first parameter.
for /F "tokens=1-6 delims=,-./: " %%A in ("%DateTime%") do (
    rem For English US date MM/DD/YYYY or M/D/YYYY
    set "Day=%%B" & set "Month=%%A" & set "Year=%%C"
    rem For German date DD.MM.YYYY or English UK date DD/MM/YYYY
    rem set "Day=%%A" & set "Month=%%B" & set "Year=%%C"
    set "Hour=%%D" & set "Minute=%%E" & set "Second=%%F"
)

rem Remove leading zeros from the date/time values or calculation could be wrong.
if "%Month:~0,1%"  EQU "0" ( if "%Month:~1%"  NEQ "" set "Month=%Month:~1%"   )
if "%Day:~0,1%"    EQU "0" ( if "%Day:~1%"    NEQ "" set "Day=%Day:~1%"       )
if "%Hour:~0,1%"   EQU "0" ( if "%Hour:~1%"   NEQ "" set "Hour=%Hour:~1%"     )
if "%Minute:~0,1%" EQU "0" ( if "%Minute:~1%" NEQ "" set "Minute=%Minute:~1%" )
if "%Second:~0,1%" EQU "0" ( if "%Second:~1%" NEQ "" set "Second=%Second:~1%" )

rem Add 12 hours for time range 01:00:00 PM to 11:59:59 PM,
rem but keep the hour as is for 12:00:00 PM to 12:59:59 PM.
if "%Add12Hours%" == "1" (
    if %Hour% LSS 12 set /A Hour+=12
)
set "DateTime="
set "Add12Hours="

rem Must use 2 arrays as more than 31 tokens are not supported
rem by command line interpreter cmd.exe respectively command FOR.
set /A "Index1=Year-1979"
set /A "Index2=Index1-30"

if %Index1% LEQ 30 (
    rem Get number of days to year for the years 1980 to 2009.
    for /F "tokens=%Index1% delims= " %%Y in ("3652 4018 4383 4748 5113 5479 5844 6209 6574 6940 7305 7670 8035 8401 8766 9131 9496 9862 10227 10592 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245") do set "Days=%%Y"
    for /F "tokens=%Index1% delims= " %%L in ("Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N") do set "LeapYear=%%L"
) else (
    rem Get number of days to year for the years 2010 to 2038.
    for /F "tokens=%Index2% delims= " %%Y in ("14610 14975 15340 15706 16071 16436 16801 17167 17532 17897 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550 21915 22280 22645 23011 23376 23741 24106 24472 24837") do set "Days=%%Y"
    for /F "tokens=%Index2% delims= " %%L in ("N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N") do set "LeapYear=%%L"
)

rem Add the days to month in year.
if "%LeapYear%" == "N" (
    for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
) else (
    for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
)

rem Add the complete days in month of year.
set /A "Days+=Day-1"

rem Calculate the seconds which is easy now.
set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"

rem Exit this subroutine
goto :EOF

罗霍表达了如何日期和时间格式设置,文件系统和Windows版本可以忽略不计的信息,通过使用<一个HREF =htt​​ps://msdn.microsoft.com/en-us/library/aa394531.aspx相对=nofollow> WMIC - Windows管理规范命令行实用程序

rojo added information on how date and time format settings, file system and Windows version can be ignored by using wmic - the Windows Management Instrumentation command-line utility.

使用的 WMIC 有固定格式的优势,为日期/时间字符串,因此,该批次code上的所有Windows计算机工程没有适应当地的日期/时间格式。

Using wmic has the advantage of fixed format for the date/time strings and therefore the batch code works on all Windows computers without adaptations to local date/time format.

另外的Windows版本不使用的 WMIC ,因为的 GetFileTime 功能(最有可能)。

Also version of Windows does not matter on using wmic because of internal usage of GetFileTime function (most likely).

文件系统成为上也在评估分钟的本地时间/文件时间偏移UTC唯一重要的。命令 WMIC FAT驱动器上的回报只是 + *** 的分钟偏移量UTC而分钟偏差是正确的的最后修改文件时, NTFS驱动器上的文件。

The file system becomes only important on evaluating also minutes offset of local time / file time to UTC. Command wmic returns on FAT drives just +*** for the minutes offset to UTC while the minutes offset is correct for the last modification file time of a file on an NTFS drive.

举例NTFS和FAT32驱动器上的同一个文件:

Example for same file on NTFS and FAT32 drive:

NTFS:  20071011192622.000000+120
FAT32: 20071011192622.000000+***

+ 120是+60分钟CET(欧洲中部时间)+ 60分钟,活跃的夏令时间总和,或者换句话说+ 120分钟CEST(欧洲中部夏令时间)。

+120 is sum of +60 minutes for CET (Central European Time) and +60 minutes for active daylight saving time, or in other words +120 minutes for CEST (Central European Summer Time).

因此​​该批次code以下不评估分钟偏移则不需要在最近一次修改文件的时间与当前时间比较文件的UTC。

Therefore the batch code below does not evaluate the minutes offset to UTC which is not needed on comparing last modification file time of a file with current local time.

进一步的文件名必须始终与 WMIC 命令行完整路径来指定。只是在当前目录中的文件或相对路径不工作的文件名指定文件名。而且每个反斜线必须与路径文件名多了一个反斜杠转义。

Further the file name must be specified always with full path in wmic command line. Just specifying the file name for a file in current directory or a file name with relative path does not work. And each backslash must be escaped with one more backslash in file name with path.

上所用的主要缺点 WMIC 与速度。批处理code上面我的电脑上需要约50毫秒根据进程监视器<完成/ A>日志(几个平均运行的)。批处理code调用下面的 WMIC 两次需要完成同样的任务大约1500毫秒完成(也平均值)。因此,使用 WMIC 上大量文件的绝对不是一个好主意,如果能够避免,因为当地的日期/时间格式是已知的。

The main disadvantage on using wmic is the speed. The batch code above needs on my computer about 50 ms to finish according to Process Monitor log (average of several runs). The batch code below calling wmic twice needs for the same task about 1500 ms to finish (also an average value). Therefore using wmic on a large number of files is definitely not a good idea if it can be avoided because local date/time format is known.

@echo off
setlocal EnableExtensions

rem Get seconds since 1970-01-01 for current date and time.
for /F "skip=1 tokens=1 delims=." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS get localdatetime') do call :GetSeconds %%T

rem Subtract seconds for 4 hours (4 * 3600 seconds) from seconds value.
set /A "CompareTime=Seconds-4*3600"

rem Define batch file itself as the file to compare time by default.
set "FileToCompareTime=%~f0"

rem If batch file is started with a parameter and the parameter
rem specifies an existing file (or directory), compare with last
rem modification date of this file.
if not "%~1" == "" (
    if exist "%~f1" set "FileToCompareTime=%~f1"
)

rem Get seconds value for the specified file.
set "DataFile=%FileToCompareTime:\=\\%"
for /F "usebackq skip=1 tokens=1 delims=." %%T in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='%DataFile%'" get lastmodified`) do call :GetSeconds %%T

rem Compare the two seconds values.
if %Seconds% LSS %CompareTime% (
    echo File %FileToCompareTime% was last modified for more than 4 hours.
) else (
    echo File %FileToCompareTime% was last modified within the last 4 hours.
)
endlocal
goto :EOF


rem Date/time format used by the command line utility of Windows
rem Management Instrumentation is always YYYYMMDDHHmmss with a
rem dot and a 6 digit microsecond value and plus/minus 3 digit
rem time offset to UTC in minutes independent on region and
rem language settings. Microsecond is always 000000 for a file
rem time. The minutes offset including time zone offset and
rem current daylight saving time offset is returned for a file
rem time only for a file on an NTFS drive. Minutes offset is
rem +*** for a file on a FAT drive (at least on Windows XP).

:GetSeconds
rem Get year, month, day, hour, minute and second from first parameter.
set "DateTime=%~1"
set "Year=%DateTime:~0,4%"
set "Month=%DateTime:~4,2%"
set "Day=%DateTime:~6,2%"
set "Hour=%DateTime:~8,2%"
set "Minute=%DateTime:~10,2%"
set "Second=%DateTime:~12,2%"
rem echo Date/time is: %Year%-%Month%-%Day% %Hour%:%Minute%:%Second%

rem Remove leading zeros from the date/time values or calculation could be wrong.
if "%Month:~0,1%"  EQU "0" ( if "%Month:~1%"  NEQ "" set "Month=%Month:~1%"   )
if "%Day:~0,1%"    EQU "0" ( if "%Day:~1%"    NEQ "" set "Day=%Day:~1%"       )
if "%Hour:~0,1%"   EQU "0" ( if "%Hour:~1%"   NEQ "" set "Hour=%Hour:~1%"     )
if "%Minute:~0,1%" EQU "0" ( if "%Minute:~1%" NEQ "" set "Minute=%Minute:~1%" )
if "%Second:~0,1%" EQU "0" ( if "%Second:~1%" NEQ "" set "Second=%Second:~1%" )

rem Must use 2 arrays as more than 31 tokens are not supported
rem by command line interpreter cmd.exe respectively command FOR.
set /A "Index1=Year-1979"
set /A "Index2=Index1-30"

if %Index1% LEQ 30 (
    rem Get number of days to year for the years 1980 to 2009.
    for /F "tokens=%Index1% delims= " %%Y in ("3652 4018 4383 4748 5113 5479 5844 6209 6574 6940 7305 7670 8035 8401 8766 9131 9496 9862 10227 10592 10957 11323 11688 12053 12418 12784 13149 13514 13879 14245") do set "Days=%%Y"
    for /F "tokens=%Index1% delims= " %%L in ("Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N") do set "LeapYear=%%L"
) else (
    rem Get number of days to year for the years 2010 to 2038.
    for /F "tokens=%Index2% delims= " %%Y in ("14610 14975 15340 15706 16071 16436 16801 17167 17532 17897 18262 18628 18993 19358 19723 20089 20454 20819 21184 21550 21915 22280 22645 23011 23376 23741 24106 24472 24837") do set "Days=%%Y"
    for /F "tokens=%Index2% delims= " %%L in ("N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N N Y N N") do set "LeapYear=%%L"
)

rem Add the days to month in year.
if "%LeapYear%" == "N" (
    for /F "tokens=%Month% delims= " %%M in ("0 31 59 90 120 151 181 212 243 273 304 334") do set /A "Days+=%%M"
) else (
    for /F "tokens=%Month% delims= " %%M in ("0 31 60 91 121 152 182 213 244 274 305 335") do set /A "Days+=%%M"
)

rem Add the complete days in month of year.
set /A "Days+=Day-1"

rem Calculate the seconds which is easy now.
set /A "Seconds=Days*86400+Hour*3600+Minute*60+Second"

rem Exit this subroutine
goto :EOF

这篇关于查找出来,如果​​文件是批处理文件老年超过4小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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