如何检查时间戳创建一个文件在Windows批处理脚本? [英] How can I check the time stamp creation of a file in a Windows batch script?

查看:128
本文介绍了如何检查时间戳创建一个文件在Windows批处理脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助怎么办检查在某些特定文件夹的文件带时间戳然后年长超过2天会删除或删除或复制到其他地方?

I need help how do check files in particular folders with time stamp older than 2 days then will remove or delete or copy to other place?

推荐答案

一个简单的循环使用 SET 使用〜牛逼修改命令返回目录中的文件的最后修改日期。

A simple FOR loop with a SET command using the ~t modifier returns the last-modified date of the files in a directory.

请参阅此例中

@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 SET 了解更多信息。

See HELP FOR and HELP SET for detailed information.

但是,对于超出了简单的对比上面显示比较日期时,你需要提取每个日期组件

But, for comparing dates beyond the simple comparison showed above, you need to extract each date component

set dd=!tf:~0,2!
set mm=!tf:~3,2!
set yyyy=!tf:~6,4!

但是,等待,在一个BAT文件中提取的日期组件是一个非常棘手的问题,因为%DATE%〜牛逼修改格式,使用短日期格式,这是完全(不休)定制的。一个用户可以配置系统返回Fri040811而另一个用户可以选择08/04/2011。这是一个为BAT程序员一个完整的噩梦。

But, wait, extracting the date components in a BAT file is a very tricky issue, because %DATE% and the ~t modifier format the date using the short-date format, that is fully (endlessly) customizable. One user may configure its system to return Fri040811 while another user may choose 08/04/2011. It's a complete nightmare for a BAT programmer.

一种可能的解决方案是临时改变的格式。看到这个例子。

One possible solution is to temporarily change the format. See this example.

@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

最后,你应该做的日期算术,你需要在年月日的日期转换为天数,这不是很明显都不是。下面是一些code ++做的这种转变。

And finally you should do the arithmetic with dates, you need to transform the date in DD MM YYYY to a number of days, which is not obvious neither. Here is some code to do this transformation.

: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 间$ p $点八进制与零开头的号码。

the strange idiom (1%MM% %% 100) is used to fix a problem with the way SET /A interprets as octal the numbers that begin with zero.

所以,把所有这些拼凑...

so, putting all those pieces together...

@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

这篇关于如何检查时间戳创建一个文件在Windows批处理脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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