Batch - xcopy files older than x minutes

3.5k Views Asked by At

after a new file creation time of 20 min I want to copy all files with the extensions .txt from Folder New to Folder Target. (I want to copy olny files are older than 20 min)

So far, it looks like this:

set hh=%TIME:~0,2%
set mm=%TIME:~3,2%
set ss=%TIME:~6,2%

set /a sum_sec=%hh% * 3600 + %mm% * 60 + %ss% -300

set /a h=%sum_sec% / 3600
set /a m=(%sum_sec%/60) - 60 * %h%
set /a s=%sum_sec% - 60 * (%sum_sec%/60)
IF %h% LSS 10 set h=0%h%
IF %m% LSS 10 set m=0%m%
IF %s% LSS 10 set s=0%s%

set new_time=%h%:%m%:%s%

So far I've only delete files that are older than 20 minutes:

forfiles /P "C:\Users\Desktop\Start" /S /M *.txt /C "cmd /c if @ftime LSS %new_time% del /F /Q @path"

My Question now is how can I copy new files from Folder New to Folder Target after 20 minutes ?

Best regs

1

There are 1 best solutions below

1
On BEST ANSWER

Check the FileTimeFilter.bat - it is capable to filter files by different time conditions.So (it should be in the same directory):

@echo off
for /f "tokens=* delims=" %%# in ('
  call FileTimeFiler.bat "New" -mm -20 -direction before -filetime created
') do (
   copy "%%~f#" "Target"
)

EDIT . Filter txt files

@echo off
for /f "tokens=* delims=" %%# in ('
  call FileTimeFiler.bat "New" -mm -20 -direction before -filetime created ^| findstr /e /i ".txt"
') do (
   copy "%%~f#" "Target"
)