Solved robocopy delete old folders by oldest date

Dell Inspiron 11z notebook - customizabl...
May 7, 2014 at 09:18:23
Specs: Windows XP, 8gb
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


See More: robocopy delete old folders by oldest date

Report •


✔ Best Answer
May 8, 2014 at 08:30:07
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.



#1
May 7, 2014 at 10:24:35
You might want to look into FORFILES. Its logic may be more to your liking.

How To Ask Questions The Smart Way


Report •

#2
May 7, 2014 at 18:30:02
Here's how forfiles would handle it:
FORFILES -M*.* -D-7 -C"CMD /C IF @ISFILE==TRUE ECHO @FILE"
Obviously, I'm not gonna put the RD /S /Q in the above, but you can when you're feeling brave. Just substitute it for the ECHO.
Essentially, all you need is a list of the dir.s to be removed per your qualifications, which is to filter: 1) only directories, not files, and 2) apply datemath to this subset and compare. Forfiles can do this, but robocopy seems to lack that option. VBscript can also do this. Once you have the list, batch can readily do the work.

message edited by nbrane


Report •

#3
May 7, 2014 at 18:50:03
Thanks for your reply. I took your solution and modified it with help from other forums.

Here's what I have.

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


Report •

Related Solutions

#4
May 7, 2014 at 19:34:39
Looks like it's ready. Test with caution, do a "dry run" with only "echo" before you acttivate the destruct sequence! (also, RD doesn't have a /d option, that I know of, but that's irrevelant anyway.)

Report •

#5
May 8, 2014 at 03:01:51
i will test in a few hours. Thanks Nbrane.

Report •

#6
May 8, 2014 at 08:30:07
✔ Best Answer
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.


Report •


Ask Question