I run a backup of each computer that gets stored in:

D:\Backup

Each computer has a subfolder.

Computer 1: D:\Backup\Computer1
Computer 2: D:\Backup\Computer2
Computer 3: D:\Backup\Computer3

Within each computer, is another folder, which is the date of the backup Computer 1: D:\Backup\Computer\20110513

Within the specific computer folder, is a group of files and subfolders inside of it.

How can I delete the backup folders that are older than 3 days old? So today is May 13, 2011, I want to delete everything that's more than 3 days old.

So a folder in

Computer 1: D:\Backup\Computer 1\20110508 

would need to be deleted.

How can I run a batch file to search inside each Computer's folder and delete all the subdirectories and files that are older than 3 days.

The name of the computer folders can change and there not consecutive numbers, so I would you would need to check the D:\Backup subdirectories that are older than 3 days, but during the deletion, it would prevent deleting the computer name folder - incase it hasn't had a backup in 3 days. I don't want the computer folder getting deleted then.

link|improve this question
feedback

migrated from stackoverflow.com May 14 '11 at 11:39

This question came from our site for professional and enthusiast programmers.

1 Answer

Forfiles from the Windows Resource Kit should be able to help you. You might find some usage examples here.

BTW: if your backups do preserve timestamps for files and directories (which should be good practice with backup copies), you obviously will run into problems here, as there is no way to determine if a backup is "3 days old" without keeping separate records for the backup date and evaluating them during your script run.

link|improve this answer
ForFiles appears to be included as standard with recent Windows versions (eg Windows 2003 server, Visa, Windows 7). – sgmoore May 14 '11 at 12:54
I use this all the time. Here's a rough draft of what you would need to do with it. forfiles /p D:\Backup\Computer1 /m * /d -60 /c "cmd /c if @ISDIR==TRUE RD /s /q @file" – Nixphoe May 14 '11 at 13:07
feedback

Your Answer

 
or
required, but never shown