开发者

batch file to copy files older than 30 minutes from one folder to another

开发者 https://www.devze.com 2023-01-06 01:14 出处:网络
How can I copy files which are older than 30 minutes from current time from one fo开发者_如何学编程lder to another one?There are several Windows ports for the *nix find command available, offering swi

How can I copy files which are older than 30 minutes from current time from one fo开发者_如何学编程lder to another one?


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…
Note that Powershell certainly is a viable alternative to achieve this goal.

In plain DOS, here is a rather limited batch script, but it suffices as a base to solve your problem:

@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 :]


The easiest way is to use robocopy (or 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

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)

h_ttp://www.petri.co.il/download_free_reskit_tools.htm

0

精彩评论

暂无评论...
验证码 换一张
取 消