
[英]Windows batch file: move files older than X minutes to a different directory using UnixUtils find command
[英]Find out if file is older than 4 hours in Batch file
很简单,我如何确定文件夹中的特定文件是否超过 X 小时? 文件名和位置将始终相同。
谢谢
在将文件时间与批处理代码进行比较时,必须考虑以下内容:
有环境变量DATE和TIME在访问时(而不是在开始执行批处理时)以取决于Windows 区域和语言设置的格式返回当前日期和时间。 国家/地区定义日期和时间格式。
甚至可以在那里定义例如带有DD.MM.YYYY
(带前导零的日期和月份)的德国短日期格式和带有HH:mm:ss
(24 小时格式,带前导零的小时、分钟和秒)的时间格式)。 但这并不意味着DATE和TIME的值真的使用这种格式。 例如,即使使用HH:mm:ss
定义的德国国家,时间是格式的H:mm:ss,ms
,这意味着在小时和第二个小时和第二,又不是零,又是在时间字符串中逗号之后的毫秒. 日期字符串也可以有或没有工作日,具体取决于区域设置。
并且有模式%~t
来获取目录或文件的最后修改日期和时间。 在命令提示符窗口中运行call /?
和for /?
并阅读为这两个命令显示的所有帮助页面以获取详细信息。 %~t
返回的日期和时间字符串的格式以及在命令DIR的输出中显示的格式也取决于 Windows 区域设置,但通常不等于变量DATE和TIME的格式。 文件或目录的最后修改时间通常在日期/时间字符串中不带第二个值返回。
最好是使用以下批处理文件找出最好在上午 10:00 之前执行的当前用户当前计算机上的格式。
@echo off
echo Current date/time: %DATE% %TIME%
for %%I in ("%~f0") do echo Batch file time: %%~tI
pause
在 Windows 文件系统上, 文件时间使用两种存储格式:
在使用 NTFS 的存储介质上,使用NTFS 精度时间,它是自协调世界时间 (UTC) 1601 年 1 月 1 日凌晨 12:00 起经过的 100 纳秒间隔的 64 位数。
在使用 FAT、FAT16、FAT32 或 exFAT 的存储介质上,文件时间使用DOS 日期时间格式存储,具有两个 16 位值,分辨率为 2 秒,并使用当前本地时间。
这意味着 FAT 介质上的文件不可能出现奇数秒,但 NTFS 介质上的文件不可能出现奇数秒。 将 NTFS 驱动器上最后修改时间奇数秒的文件复制到 FAT32 驱动器会导致相同文件的最后修改时间相差 1 秒。
NTFS 媒体上的文件时间以 UTC 格式存储。 因此%~t
或命令DIR返回的内容以及例如在 Windows 资源管理器中显示的内容取决于
更改这 4 个参数中的任何一个都会立即更改 NTFS 存储介质上文件和目录的所有显示文件时间。
FAT 媒体上的文件时间或多或少是恒定的,因为在创建、修改或上次访问时使用本地时间,但请进一步阅读以了解详细信息。
由于 NTFS 媒体上的文件时间以 UTC 存储,但以本地时间显示,因此当前时区和该时区的夏令时对于文件时间比较很重要,尤其是在对 NTFS 和 FAT32 驱动器上的文件进行比较时。
但对于 FAT 媒体上的文件,时区和夏令时设置可能很重要,具体取决于 Windows 版本。 阅读下文了解详情。
有自动调整夏令时的设置,这在默认启用时变得很重要,并且当前设置的时区定义了夏令时调整。
例如,夏令时目前在欧洲很有效。 在 Windows 的时钟设置中禁用此选项会导致 NTFS 媒体上显示的文件时间立即更改 -1 小时,并且 FAT 媒体上的许多文件也取决于 Windows 的版本。
在比较 NTFS 和 FAT 介质上的文件时间时,必须考虑到由夏令时调整引起的 1 小时时差。
所有 Windows NT 版本都支持 NTFS。 文件时间行为对于所有支持 NTFS 的 Windows 版本上的文件和目录都是相同的。
但是如何解释 FAT 介质上的文件时间取决于 Windows 的版本。 以前的 Windows Vista FAT 媒体上的文件时间独立于时区的更改、夏令时设置和夏令时的自动调整。 文件的最后修改日期/时间在 Windows 2000 和 Windows XP 上是恒定的。
但在 Windows Vista 和更高版本上,FAT 媒体上显示的文件时间取决于夏令时和夏令时的自动调整。 时区没有直接关系。 选择当前使用的另一个时区不会像更改 NTFS 介质上的文件那样更改 FAT 介质上文件的显示文件时间。 但是如果当前时间在活动时区的夏令时范围内,并且启用了夏令时的自动调整,并且文件或目录的本地时间在夏令时范围内,则Windows Vista会增加+1小时然后。 在标准时间内最后一次在 FAT 驱动器上修改的文件在一年中保持不变; 仅显示在 DST 期间最后修改的文件,根据启用自动 DST 调整的当前时间,显示有或没有 +1 小时。
例如,当 Windows 7 计算机上的 FAT32 驱动器上的文件夹的公共共享并使用 Windows XP 计算机连接到此公共文件夹时,这种不同的 FAT 文件时间管理可能会非常混乱。 上次修改于 2015 年 9 月 19 日 17:39:08 存储在 Windows 7 PC 上的 FAT32 驱动器上的文件今天由 Windows 7 显示,文件时间为 19.09.2015 17:39:08,文件时间为 19.09.2015 17:39:08,但在 2 个月内未使用 19.09.2015 16:39:08 进行修改,并且 Windows XP 今天和将来在连接的 Windows 7 PC 上显示相同的文件,文件时间为 19.09.2015 17:39:08。
将存档(ZIP、RAR、...)中存储的文件的文件时间与 NTFS 或 FAT 媒体上的文件进行比较可能真的是一场噩梦。
对于这个比较文件的最后修改时间和当前时间的任务,只需考虑日期和时间格式设置就足够了。
下面的批处理代码使用我在Batch file 的答案中详细解释的代码来删除早于 N 天的文件。 在使用下面的代码之前应该阅读这个解释。
根据变量DATE和TIME的日期/时间格式以及%~tI
为本地 Windows PC 上的文件返回的日期/时间字符串,可能需要对代码进行少量修改。 在批处理代码的注释行中给出了适当的提示。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
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 not "%DateTime: AM=%" == "%DateTime%" (
set "DateTime=%DateTime: AM=%"
) else if not "%DateTime: PM=%" == "%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%" == "0" if not "%Month:~1%" == "" set "Month=%Month:~1%"
if "%Day:~0,1%" == "0" if not "%Day:~1%" == "" set "Day=%Day:~1%"
if "%Hour:~0,1%" == "0" if not "%Hour:~1%" == "" set "Hour=%Hour:~1%"
if "%Minute:~0,1%" == "0" if not "%Minute:~1%" == "" set "Minute=%Minute:~1%"
if "%Second:~0,1%" == "0" if not "%Second:~1%" == "" 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 two 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
rojo添加了有关如何使用wmic (Windows Management Instrumentation 命令行实用程序)忽略日期和时间格式设置、文件系统和 Windows 版本的信息。
使用wmic具有日期/时间字符串格式固定的优点,因此批处理代码适用于所有 Windows 计算机,而无需适应本地日期/时间格式。
由于GetFileTime函数的内部使用(很可能),使用wmic的 Windows 版本也无关紧要。
文件系统仅在评估本地时间/文件时间与 UTC 的分钟偏移量时才变得重要。 命令wmic在 FAT 驱动器上仅返回+***
表示与 UTC 的分钟偏移量,而分钟偏移量对于 NTFS 驱动器上文件的最后修改文件时间是正确的。
NTFS 和 FAT32 驱动器上相同文件的示例:
NTFS: 20071011192622.000000+120
FAT32: 20071011192622.000000+***
+120 是 CET(中欧时间)的 +60 分钟和有效夏令时的 +60 分钟的总和,即 CEST(中欧夏令时)的 +120 分钟。
因此,下面的批处理代码不会评估与 UTC 的分钟偏移量,这在将文件的最后修改文件时间与当前本地时间进行比较时不需要。
此外,必须始终在wmic命令行中使用完整路径指定文件名。 仅指定当前目录中文件的文件名或具有相对路径的文件名是行不通的。 并且每个反斜杠必须在带有路径的文件名中再使用一个反斜杠进行转义。
使用wmic的主要缺点是速度。 根据进程监视器日志(几次运行的平均值),上面的批处理代码在我的计算机上需要大约 50 毫秒才能完成。 下面调用wmic两次的批处理代码需要大约 1500 毫秒才能完成相同的任务(也是平均值)。 因此,在大量文件上使用wmic绝对不是一个好主意,如果可以避免,因为本地日期/时间格式是已知的。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Get seconds since 1970-01-01 for current date and time.
for /F "tokens=2 delims==." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') 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 tokens=2 delims==." %%T in (`%SystemRoot%\System32\wbem\wmic.exe DATAFILE where "name='%DataFile%'" GET LastModified /VALUE`) 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% == 0 set "Month=%Month:~1%"
if %Day:~0,1% == 0 set "Day=%Day:~1%"
if %Hour:~0,1% == 0 set "Hour=%Hour:~1%"
if %Minute:~0,1% == 0 set "Minute=%Minute:~1%"
if %Second:~0,1% == 0 set "Second=%Second:~1%"
rem Must use two 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
或者,您可以使用 C# 创建一个简单的程序,例如:
// compile using c:\Windows\Microsoft.NET\Framework\v3.5\csc FileByAge.cs
using System;
using System.IO;
public static class Program
{
public static void Main(string[] args)
{
double age = double.Parse(args[0]);
string fileName;
while ((fileName = Console.ReadLine()) != null)
{
if(age >= (DateTime.Now - new FileInfo(fileName).CreationTime).TotalHours)
{
continue;
}
Console.WriteLine(fileName);
}
}
}
然后可以使用该程序在批处理文件中查找所有超过 2 小时的文件,如下所示:
for /F "delims=" %f in ('dir /B *.txt^|FileByAge.exe 2') do echo %f
正如@Paul 所提到的,甚至可以构建一个可以自行编译和运行的 hacky 混合批处理/c# 文件:
@echo off
setlocal
set CsFile=%TEMP%\%~n0.cs
set ExeFile=%TEMP%\%~n0.exe
pushd %SystemRoot%\Microsoft.NET\Framework
for /F %%f in ('dir /B /O:-N v*') do cd %%f & goto :BreakLoop;
:BreakLoop
(echo /* & type "%~f0") > "%CsFile%"
csc.exe /nologo /out:"%ExeFile%" "%CsFile%"
del "%CsFile%"
popd
more | "%ExeFile%" %*
del "%ExeFile%"
endlocal
goto:eof
*/
using System;
using System.IO;
public static class Program
{
public static void Main(string[] args)
{
var olderThen = args[0].Contains("older");
var targetAge = double.Parse(args[1]);
string fileName;
while ((fileName = Console.ReadLine()) != null)
{
if (string.Empty == fileName)
{
continue;
}
var fileAge = (DateTime.Now - new FileInfo(fileName).CreationTime).TotalHours;
if (olderThen ? targetAge > fileAge : targetAge < fileAge)
{
continue;
}
Console.WriteLine(fileName);
}
}
}
并且可以这样使用:
for /F "delims=" %f in ('dir /B *.txt^|FileByAge.bat older 2') do echo %f
纯批次在日期数学上很糟糕。 如果您的脚本在午夜后不久运行会发生什么? 如果时间检查跨越夏令时更改怎么办?
我建议调用 Windows Script Host 并借用一种可以更好地处理日期/时间数学的语言。 这是一个混合批处理/JScript 脚本,可以很容易地计算文件或目录的年龄。 使用 .bat 扩展名保存它并试一试。
@if (@CodeSection==@Batch) @then
@echo off
setlocal
set "file=C:\Path\To\File.ext"
for /f %%I in ('cscript /nologo /e:JScript "%~f0" "%file%"') do (
rem // value contained in %%I is in minutes.
if %%I gtr 240 (
echo %file% is over 4 hours old.
rem // do stuff here to take whatever actions you wish
rem // if the file is over 4 hours old.
)
)
goto :EOF
@end // end batch portion / begin JScript hybrid chimera
var fso = new ActiveXObject("scripting.filesystemobject"),
arg = WSH.Arguments(0),
file = fso.FileExists(arg) ? fso.GetFile(arg) : fso.GetFolder(arg);
// Use either DateCreated, DateLastAccessed, or DateLastModified.
// See http://msdn.microsoft.com/en-us/library/1ft05taf%28v=vs.84%29.aspx
// for more info.
var age = new Date() - file.DateLastModified;
WSH.Echo(Math.floor(age / 1000 / 60));
有关批处理/JScript 混合脚本的更多信息,请参阅此 GitHub 页面。 上面使用的样式类似于该页面上的 Hybrid.bat。 这个答案是基于另一个. 就其价值而言,PowerShell 还擅长处理日期数学; 但 WSH 执行得更快。
对此的解决方案将很大程度上取决于预期的时间范围,因为 AFAIK 在没有外部程序的情况下,您不会在批处理文件中将时间作为一个整数。 这意味着您必须在脚本中解析时间值,并且解析量和所需的计算次数取决于您需要它工作的范围。 这是一个简单的解决方案。 它假定文件不能超过 24 小时。
set "filename=myfile.txt"
rem extract current date and time
for /f "tokens=1-5 delims=.:, " %%a in ("%date% %time%") do (
set day=%%a&set mon=%%b&set yr=%%c&set hr=%%d&set min=%%e
)
rem extract file date and time
for /f "tokens=1-5 delims=.:, " %%a in ('"dir %filename%|find "%filename%""') do (
set fday=%%a&set fmon=%%b&set fyr=%%c&set fhr=%%d&set fmin=%%e
)
rem calculate age of file (in minutes)
set /a "age=((hr*60+min)-(fhr*60+fmin)+(24*60))%%(24*60)"
set /a "max=4*60"
if %age% geq %max% echo.file is older than 4 hours
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.