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

3 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
  2. I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... Download Folder Lock Regarding PC Desktop - Don't Spend Your Time Searching, Go through Exactly About Mobile Desktops Right here password protecting photos on iphone

    ReplyDelete