A simple batch script to delete Folders older than x


I had a very simple task I wanted automated:

Delete all folders in a directory older than 4 days.

As it was a simple task I was looking for an EQUALLY simple solution to run via a batch script in Windows, and was surprised how many different approaches, comments, solutions and semi-solutions are out there.  And man, just google around and see how many people were writing complicated or infinitely configurable solutions.  Great for them, but really I just want to trash these old folders.

So, even though I could, I didn’t want to install any third party tools (no matter how “COOL”), write a C# program, delve into VBScript – as all this was way more complicated than this job needed.  Basically just wanted to create a one or 2 line .bat file that would delete the folders without any elaborate conditionals on empty folders, file extension filtering…  just a single damn hard coded “nuke the old folders” script.

What I found was that a DOS script really doesn’t seem to provide easy ways to do conditionals around timestamps.  There are a lot of resources on the web where people are attempting to achieve this, and I spent way too much time tracking their progress across multiple threads and blogs.

So I hunkered down and just wrote something with forfiles.  It’s is available on my Windows 2007 server, and was able to handle the nuking in a very simple one line statement.

ForFiles /P C:backups  /D -4 /C “CMD /C if @ISDIR==TRUE echo RD /Q @FILE &RD /Q /S @FILE”

I saved this into a batch file deleteOldBackups.bat and scheduled as a daily system task.

Some notes:

  • you can change the path: /P C:backups
  • set the date interval (here it’s set to delete anything more than 4 days old) : /D -4  
  • Forfiles date functions appear to work off the modified date rather than the created date, but this works fine for me as the nightly batch script that copies folder into C:backups always sets the modified dates for those folders to the date that it was copied.

There are a lot of switches and parameters that you can use to apply conditional logic to. Check out msdn here, http://technet.microsoft.com/en-us/library/cc753551(WS.10).aspx

13 thoughts on “A simple batch script to delete Folders older than x

  1. Fantastic little script, thank you for sharing.

    I’ve made a simple modification to allow it to delete folders from a UNC path, it looks something like this:

    PushD “\servershare” &&(
    “forfiles.exe” /D -0 /C “CMD /C if @ISDIR==TRUE echo RD /Q @FILE &RD /Q /S @FILE”
    ) & PopD

    Thank you again.

    Mark

    ) & PopD

  2. I found if you copy and paste your code it throws a syntax error when running the script(/c only allowed 1 times) If you type the code in yourself, the error is removed.

    • Joe, all you have to change are the quotation marks from a copy/paste. I got the same error, but just changing those fixed it nicely. :)

      • Thanks very much. (the copy/paste thing is weird!)
        Apart from all the time I spent looking for this post, it seems nearly as easy as linux! well done for this post.
        Now how about a 1 liner in windows to diff remote web pages to check for content changes?

  3. Hi There.

    I need to use this script but i can’t work it out, i did a verbatim cut and paste into a new .BAT file and i run it from command prompt (so i can see errors)
    and i get this
    ERROR: Invalid syntax. ‘/C’ option is not allowed more than ’1′ time(s).

    The actual script i used is ….
    ForFiles /P C:backups /D -20 /C “CMD /C if @ISDIR==TRUE echo RD /Q @FILE &RD /Q /S @FILE”

    i copied a windows temp folder to C:backups so i could safely test… there are files and folders older than 20 days ….

    Thanks

Leave a Reply