1

How can I copy files which are older than 30 minutes from current time from one folder to another one?

1
  • 3
    Do you really want DOS or do you want the Windows command shell?
    – paxdiablo
    Jul 6, 2010 at 10:46

2 Answers 2

3

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

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.