Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

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

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

add comment

2 Answers

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.

share|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
    
Technet link for forfiles is technet.microsoft.com/en-us/library/cc753551(v=WS.10).aspx –  John M Jun 7 '12 at 18:32
    
@JohnM thank you for pointing out. I've edited the answer to incorporate it. –  the-wabbit Jun 8 '12 at 6:25
add comment

Additionally - here is a test that doesn't delete.

forfiles /m * /d -60 /c "cmd /c if @ISDIR==TRUE ECHO HI_MOM @FNAME"

And the dangerous version. This version searchs in the folder DELL and deletes all folders that are 60 days old or older.

forfiles /p C:\DELL /m * /d -60 /c "cmd /c if @ISDIR==TRUE RD /S /Q @FILE"

The RD syntax is /S for remove all directories and files. /Q is quiet mode (doesn't ask).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.