computing
  • 3

Solved Robocopy Delete Old Folders By Oldest Date

  • 3

I am working on a backup script to delete old folders from a folder.My backup folder looks like this (backup_mm_dd_year is a folder)

C:\backups\backup_5_1_2014
C:\backups\backup_5_2_2014
C:\backups\backup_5_3_2014
C:\backups\backup_5_4_2014
C:\backups\backup_5_6_2014
C:\backups\backup_5_7_2014
C:\backups\backup_5_8_2014
C:\backups\backup_5_9_2014
C:\backups\backup_5_10_2014
C:\backups\backup_5_12_2014

I was looking at using robocopy to delete folders older than say 7 days. That is, let’s say I run a script on day 13, it should delete any backup_mm_dd_yy older than 7 days. that means it should delete backup 5_1-3_2014.

I see that robocopy have a switch — /maxage:n — exclude files older than n days/date. Is there anyway to tell robocopy to simply check at the folder level instead of at the file level?

In essence, I simply want robocopy to check /maxage:n at the folder level instead of checking at the file level.

Any help will be greatly appreciated.

message edited by xirsteon

Share

1 Answer

  1. Ok I did a test run and it worked. Final script is

    FORFILES /p C:\backupfiles -D -7 -C “CMD /C IF @ISDIR==TRUE ECHO rd /q /s @FILE&rd; /q /s @FILE”

    /p C:\backupfiles –> path to check for old backup files

    -D -7 –> delete any folders more than 7 days old (uses modified date instead of created date)

    Thanks again.

    • 0