Below is a script of how to MOVE files to a folder based on their age:

ROBOCOPY <source> <destination> /MOVE /E /MINAGE:<number_of_days> /CREATE /R:1 /W:1 /XD <folders_you_want_to_omit>

Sample:

ROBOCOPY E:\ E:\_ToBeDeleted /MOVE /E /MINAGE:21 /CREATE /R:1 /W:1 /XD E:\COMPANY_PICS E:\_ToBeDeleted

The translation of this command is:

  • ROBOCOPY E:\ – look at the E: Drive, we could have entered a drive letter AND a folder for this instead
  • E:\_ToBeDeleted – apply the results to the E:\_ToBeDeleted  folder
  • /MOVE – move the files
  • /E – everything including empty directories
  • /MINAGE:21 – only files that are 21 days or older – a useful alternative here is /MINLAD which is Last Access Date
  • /CREATE – create the folder structure and move the files
  • /R:1 – retry once if the file is in use
  • /W:1 – wait 1 second if the file is in use
  • /XD – exclude the COMPANY PICTURES folder and the destination folder

Another way to handle this is using the FORFILES command which we have documented: urtech.ca/2015/07/solved-command-line-batch-to-delete-files-based-on-their-age/


5 Comments

Lubos Hrasko · January 25, 2020 at 11:39 am

TIP – test any robocopy command without the /MOVE flag to make sure files are being copied correctly before first

vikas khandola · March 21, 2018 at 5:13 am

use robocopy

Randall · August 14, 2017 at 1:34 pm

BE CAREFUL!!! I did this and was left with nothing but 0 byte files in the _ToBeDeleted folder.

    Suleman · September 13, 2022 at 2:08 am

    Randall,
    by running the following command, all files & folder copied… but size 0 byte.

    robocopy source Destination /CREATE /E /minage:30

    what is the root cause and fix ?

      Chris · December 27, 2022 at 12:38 pm

      the CREATE flag creates the moved files as 0 size. No fix.

      To move the full files, omit the /create

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *