Tuesday, December 4, 2012

Delete files / folders older than X days on windows (bat file)

This script will delete all MS SQL database backups (*.bak files) older than 30 days on the current directory and all its subdirectories:

forfiles /s /m *.bak /d -30 /c "cmd /c del @path"


This script will delete all MS SQL database backups (*.bak files) older than 30 days on a specified directory and all its subdirectories:

forfiles /p "c:\database\backups" /s /m *.bak /d -30 /c "cmd /c del @path"



This script will delete all folders older than 30 days on a specified directory:


forfiles /p "C:\todelete" /m * /d -30 /c "cmd /c rd /s /q @path"  


More from here: ForFiles

If you don't have the forfiles.exe, you can download it from here: ForFiles.zip

2 comments:

  1. Replies
    1. hi ner0, it should work.. these scripts are still working fine on our servers.. could you please check your permissions to execute your file? were you trying to run it as scheduled task?

      Delete