0

How can I copy files that were created within X days using batch script?

I am trying to make a daily backup batch for files created or modified within last 45 days, but I can't seem to find anything about copying files that were created within 45 days.

Is there any (preferably easy) way to copy files created or modified within X days and purging ones that were created or modified before X days in a batch?

So far I have this:

::Delete older than 45 days:
forfiles /p S:\ /s /D -45 /C "cmd /c del /a rhiosal/q @path&echo @path"

::Clear out empty folders:
robocopy S:\D S:\D /S /move
robocopy S:\E S:\E /S /move

::Copy modified withing 45 days:
ROBOCOPY "D:" "S:\D" *.* /S /DCOPY:T /COPYALL /MAXAGE:45 /ETA /PURGE /XD "$recycle.bin" /XD "System Volume Information"
ROBOCOPY "E:" "S:\E" *.* /S /DCOPY:T /COPYALL /MAXAGE:45 /ETA /PURGE /XD "$recycle.bin" /XD "System Volume Information"

Thank you.

Maredha
  • 3
  • 3
  • *N. B.:* Alter the `forfiles` command line to `forfiles … /C "cmd /C if @isdir==FALSE del …"` since `forfiles` returns both files and directories; and both `forfiles` and `robocopy` regard the *last modification* date… – aschipfl Jun 19 '20 at 17:28
  • Does this answer your question? [Batch file to delete files older than N days](https://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days) – Nico Nekoru Jun 19 '20 at 19:14
  • @aschipfl I figured that forfiles would regard last modification date, just didn't have the time to test it. Thanks for the dir note. – Maredha Jun 19 '20 at 20:36
  • @NekoMusume The qestion you posted link to doesn't help because it uses only modified within X days, not created within X days like in my question. – Maredha Jun 19 '20 at 20:38

0 Answers0