[英]batch file to copy files older than 30 minutes from one folder to another
如何将当前时间超过30分钟的文件从一个文件夹复制到另一个文件夹?
There are several Windows ports for the *nix find
command available, offering switches like -mmin
and -mtime
that would be useful here, allowing the problem to be solved with a one-liner… 有几个Windows端口可供* nix find
命令使用,提供-mmin
和-mtime
等开关,这在这里很有用,可以通过单行解决问题...
Note that Powershell certainly is a viable alternative to achieve this goal . 请注意,Powershell当然是实现这一目标的可行替代方案。
In plain DOS, here is a rather limited batch script, but it suffices as a base to solve your problem: 在普通的DOS中,这是一个相当有限的批处理脚本,但它足以作为解决问题的基础:
@echo off
setlocal enabledelayedexpansion
call :GetRefTimestamp -30
for %%f in (*) do (
call :GetFileTimestamp "%%~tf"
if "!filetimestamp!" LSS "!reftimestamp!" echo -- %%f is older than 30 minutes
if NOT "!filetimestamp!" LSS "!reftimestamp!" echo ++ %%f is NOT older than 30 minutes
)
endlocal
goto :EOF
:GetRefTimestamp
::get current date/time
for /f "usebackq tokens=1-5 delims=/:, " %%f in (`echo %DATE:~-10% %TIME: =0%`) do set reftimestamp=%%h%%g%%fT%%i%%j
::apply delta (format [-]HHMM) on time part - not handling over/underflow
set /a timedelta=%~1
set timedeltasign=
if %timedelta% LSS 0 set timedeltasign=-
set timeHHMM=%timestamp:~-4%
set /a timeHHMM+=timedelta
set /a timeMM=timeHHMM %% 100
if %timeMM% GEQ 60 set /a timeHHMM+=%timedeltasign%40
set timeHHMM=000%timeHHMM%
set reftimestamp=%reftimestamp:~0,-4%%timeHHMM:~-4%
goto :EOF
:GetFileTimestamp
::get file date/time
for /f "usebackq tokens=1-5 delims=/:, " %%f in (`echo %~1`) do set filetimestamp=%%h%%g%%fT%%i%%j
goto :EOF
Just use common sense for the delta (knowing the limitations) and refrain from using leading zeroes :] 只需使用delta的常识(知道限制)并避免使用前导零:]
The easiest way is to use robocopy (or forfiles) 最简单的方法是使用robocopy(或forfiles)
robocopy is part of win2003 rtk and is installed on vista and windows 7 by default -> http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en robocopy是win2003 rtk的一部分,默认安装在vista和Windows 7上 - > http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en
FORFILES is part of Windows 2000/NT resource kit but works fine on XP/Vista/7 -> (I think it is not available on microsoft site anymore) FORFILES是Windows 2000 / NT资源工具包的一部分,但在XP / Vista / 7上工作正常 - >(我认为它不再在微软网站上提供)
h_ttp://www.petri.co.il/download_free_reskit_tools.htm h_ttp://www.petri.co.il/download_free_reskit_tools.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.