Windows – Delete directories in windows by date

windows

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.

Best Answer

Forfiles which comes with the stock install on from Windows Vista / Server 2008 and is available from the Windows Resource Kit for earlier versions 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.