将当前日期与批处理文件中的文件修改日期进行比较 [英] Compare current date with file modified date in batch file
问题描述
我需要创建一个批处理文件,并且该文件应在Windows 2003 Server中工作.
I have a requirement to create a batch file and that should work in windows 2003 server.
- 我有源文件夹"c:\ Source"和目标文件夹"c:\ Destination".
-
我想检查源文件夹中所有文件的修改日期:
- I have source folder "c:\Source" and destination folder "c:\Destination".
I want to check all the file modified dates in source folder:
- 如果文件修改日期小于当前日期,则该文件将移动到目标文件夹中
- 否则,什么也不做.
注意:,因为我的服务器是生产服务器,所以我无法安装Resource kit和robocopy.exe.唯一的方法是编写批处理脚本.
Note: as my server is production server so, I am unable to install Resource kit and robocopy.exe. The only way is write the batch script.
Robocopy
和forfiles
在Windows2003服务器中不起作用.
Robocopy
and forfiles
are not working in Windows2003 server.
推荐答案
更新
因为您的服务器上有forfiles
,所以这很容易.要检查早于1天的文件,只需使用forfiles /D -1
.对于超过2天的文件,请/D -2
.
Update
Since you have forfiles
on your server, this is easy. To check for files older than 1 day old, just use forfiles /D -1
. For files over 2 days old, /D -2
.
forfiles /D -2 /M *.log /P C:\Source /C "cmd /c move @file c:\Destination"
在控制台窗口中输入forfiles /?
以获得完整语法.
Enter forfiles /?
in a console window for full syntax.
考虑修改您的要求.代替说如果文件修改日期小于当前日期",您应该说如果文件修改日期不等于当前日期".这使您不必进行日期数学运算,并使您的任务变得更加简单.毕竟,上次修改的日期不会是将来的日期,对吧?
Consider revising your requirement. Instead of saying "If file modified date is less than the current date", you should say, "If file modified date does not equal current date". That absolves you from having to do date math, and makes your task profoundly simpler. After all, the last modified date is not going to be a date in the future, right?
之后,只需简单地从%date%
中刮取今天的日期,然后将其与for
循环中每个文件的%%~tX
替换属性中的日期进行比较即可.在控制台窗口中键入help for
,然后查看最后两页,以获取有关此语法的更多信息.
After that, it's a simple matter of scraping today's date from %date%
and comparing it with the date from each file's %%~tX
substitution property in a for
loop. Type help for
in a console window and see the last two pages for more information about this syntax.
我认为与此有关的语言环境日期格式不会出现任何问题.只要系统的%date%
变量的格式为dayOfWeek Date
且%%~tX
的格式为Date Time etc.
,则此脚本应该起作用,而不管您是本地处理日期还是MM/DD/YYYY
还是YYYY/MM/DD
或
I don't think there will be any locale date formatting issues with this. As long as your system's %date%
variable is in the format of dayOfWeek Date
and %%~tX
is formatted as Date Time etc.
then this script should work regardless of whether you handle dates locally as MM/DD/YYYY
or YYYY/MM/DD
or DD/MM/YYYY
or something else. I hope.
@echo off
setlocal enableextensions
set "source=c:\Source"
set "destination=c:\Destination"
:: store today's date in %today%
for /f "tokens=2" %%I in ('echo %date%') do set "today=%%I"
for %%I in ("%source%\*") do (
rem :: scrape MM/DD/YYYY from %%~tI
for /f %%a in ('echo %%~tI') do (
rem :: compare the two dates
if "%%a" neq "%today%" (
echo %%~nxI: %%a does not equal %today%. Moving.
>NUL move /y "%%~fI" "%destination%"
) else (
echo %%~nxI: %%a equals %today%. Skipping.
)
)
)
这篇关于将当前日期与批处理文件中的文件修改日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!