我需要帮助如何检查特定文件夹中的时间戳超过2天的文件,然后删除或删除或复制到其他地方?如何在Windows批处理脚本中检查文件的时间戳创建?
4
A
回答
0
不确定删除,但可以使用RoboCopy(它是Windows 7的一部分)。参数:/ MAXAGE:n会复制比n更早的文件 - 我通常会将副本复制到备份文件夹中,稍后我会确定从该目录中删除所有文件。
希望这会有所帮助。
3
使用~t
修饰符的简单FOR
循环与SET
命令返回目录中文件的上次修改日期。
参见本实施例中
@echo off
setlocal enabledelayedexpansion
echo Files changed today %date%
FOR %%A IN (*.*) DO (
set tf=%%~tA
set fd=!tf:~0,10!
if !fd!==%date% (
echo %%F !tf!
)
)
详细信息,请参见HELP FOR
和HELP SET
。
但是,对于超出了简单的比较,上述结果显示比较日期时,你需要提取每个日期组件
set dd=!tf:~0,2!
set mm=!tf:~3,2!
set yyyy=!tf:~6,4!
但是,等待,在一个BAT文件中提取日期组件是一个非常棘手的问题,因为%DATE%
和~t
修饰符使用短日期格式来格式化日期格式,这是完全(无休止地)可定制的。一个用户可以将其系统配置为返回Fri040811,而另一个用户可以选择08/04/2011。这对于BAT程序员来说是一个完整的噩梦。
一个可能的解决方案是临时更改格式。看到这个例子。
@echo off
echo System Date Time = %date% %time%
reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
reg add "HKCU\Control Panel\International" /v sShortDate /d "yyyy-MM-dd" /f >nul
reg add "HKCU\Control Panel\International" /v sTimeFormat /d "HH:mm:ss" /f >nul
echo Normalized Date Time = %date% %time%
set dd=%date:~8,2%
set mm=%date:~5,2%
set yyyy=%date:~0,4%
reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul
最后你应该做的日期算术,你需要在年月日的日期转换为天数,这不是很明显都不是。这里有一些代码来做这个转换。
:days
:: Algorithm based on Fliegel-Van Flandern algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
SET /A Month1 = (1%MM% %% 100 - 14)/12
SET /A Year1 = %YYYY% + 4800
SET /A days = 1461 * (%Year1% + %Month1%)/4 + 367 * ((1%MM% %% 100) - 2 -12 * %Month1%)/12 - (3 * ((%Year1% + %Month1% + 100)/100))/4 + (1%DD% %% 100) - 32075
SET Month1=
SET Year1=
goto :eof
奇怪的成语(1%MM% %% 100)
来解决问题与方法SET /A
解释为八进制与零开头的号码。
所以,把所有那些拼凑...
@echo off
setlocal enabledelayedexpansion enableextensions
reg copy "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
reg add "HKCU\Control Panel\International" /v sShortDate /d "yyyy-MM-dd" /f >nul
reg add "HKCU\Control Panel\International" /v sTimeFormat /d "HH:mm:ss" /f >nul
set dd=%date:~8,2%
set mm=%date:~5,2%
set yyyy=%date:~0,4%
call :days
set /a today=!days!
FOR %%A IN (*.*) DO (
set tf=%%~tA
set fd=!tf:~0,10!
set dd=!fd:~8,2!
set mm=!fd:~5,2!
set yyyy=!fd:~0,4!
call :days
set /a age= !today!-!days!
if !age! leq 2 (
echo %%A is !age! days old
)
)
reg copy "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul
goto :eof
:days
:: Algorithm based on Fliegel-Van Flandern algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
SET /A Month1 = (1%MM% %% 100 - 14)/12
SET /A Year1 = %YYYY% + 4800
SET /A days = 1461 * (%Year1% + %Month1%)/4 + 367 * ((1%MM% %% 100) - 2 -12 * %Month1%)/12 - (3 * ((%Year1% + %Month1% + 100)/100))/4 + (1%DD% %% 100) - 32075
SET Month1=
SET Year1=
goto :eof
0
here is the reference of how you can delete files older than 2 days
继cmd命令将做到这一点。
forfiles /p "c:\path" /s /m *.* /d -365 /c "cmd /c del @file"
相关问题
- 1. 如何检查Windows批处理脚本中文件的大小?
- 2. 如何创建在目录中的每个文件夹中创建文本文件的脚本? (Windows批处理)
- 3. 批处理脚本重置所有文件的时间戳
- 4. 如何获得UNIVERSAL Windows批处理文件时间戳
- 5. 如何使用批处理脚本检查时间的格式?
- 6. Windows批处理:从字符串创建时间戳
- 7. 如何检查文件/目录是否在批处理脚本
- 8. 在批处理脚本中获取文件夹的创建日期和时间
- 9. 在批处理脚本中生成带时间戳的唯一文件名
- 10. 批处理文件批处理文件检查打开的浏览器脚本
- 11. 在日期间查找创建/访问/修改的文件,批处理脚本
- 12. 如何在shell脚本中运行windows批处理文件?
- 13. 学习Windows批处理文件脚本
- 14. 在Windows批处理脚本
- 15. 在Windows批处理脚本
- 16. Windows批处理脚本 - 在
- 17. 在Windows批处理脚本
- 18. 在Windows 7中使用批处理脚本检查主机文件的实体?
- 19. 在批处理作业中创建文件名作为时间戳
- 20. Windows,批处理文件时间条件
- 21. 如何从Advantage SQL脚本创建批处理文件?
- 22. 如何创建批处理文件来执行sql脚本
- 23. 如何比较windows批处理脚本中的文件格式
- 24. 在批处理脚本中检查文件大小
- 25. 如何在Windows批处理文件中创建无限循环?
- 26. 在批处理脚本之间显示的批处理脚本
- 27. 创建Windows批处理文件以创建文件夹/文件
- 28. 批处理脚本检查和清理
- 29. 在windows批处理脚本中使用FOR LOOP创建多个文件
- 30. 如何在Windows上从批处理文件运行FTP脚本?
IMO此问题属于serverfault – onof
您的意思是创建时间戳或修改时间戳? –
@onof,IMO这是一个关于编程bat文件的问题。 –