Need batch to delete folders by age

Posted on 2009-04-24
Last Modified: 2013-11-11
Hello, I am looking for a "simple" batch file that will delete any folder in the root of the drive, older than "x" days - but not the subfolders.

My client insists on saving her data onto a large USB drive. I schedule a batch file to run similar to the one below:

          md "G:\%DATE%"
          robocopy /mir C:\Data "G:\%DATE%\data"

This runs each week and puts a copy of "data" into a folder under G:\ named after the date it runs.
          i.e. "G:\Fri 04-24-2009\data"

In 12 weeks the drive is full. I need a script that will delete any folder - in the root - created more than 60 days ago (8 weeks). I do not want to delete sub folders older than 8 weeks due to most of the data in the sub folders are themselves older that 90 days.  This allows the client to have 2 months worth of backups at one week increments.

Can this be done via the command line?  What I am looking for is a line I can add to a batch file, or a Resource Tool Kit tool, that I could use for many clients.    I know a VB Script would work, unfortunately my strength is with batch files not VB .  If all you have is a VB script Id gratefully accept that too.

Question by:ProtocolTech
    LVL 12

    Expert Comment

    by:Gary Dewrell
    Found this answer that shows how to delte directories older than 5 days. You can modify it for your purposes.

    Expert Comment

    You can call the vbscript from within a batch file.  This one has a safety to ensure that you are working with the root folder of the logical drive.

    Dim fs
    Set fs = CreateObject("Scripting.FileSystemObject")

    ' set path to root of logical drive
    rootfolderpath = "G:\"
    ' set number of days for threshold to purge folder
    purgeafter = 60

    set folder = fs.GetFolder(rootfolderpath)
    ''Add single quote before next line to supress pop-up message
    wscript.echo folder.path

    ' check to ensure this is a root folder...otherwise exit
    if folder.isrootfolder=true then

          For each item in folder.SubFolders

                set subfolder = fs.getfolder(item.path)

                ' comparing age based on days since created date (not modified or accessed)
                age =  datediff("d",subfolder.datecreated,date)

                ' if age is greater than threshold number of days... force delete
                if age > purgeafter then
    ''Add single quote before next line to supress pop-up message
    wscript.echo subfolder.path & " is " & age & " days old.  purging..."
                      subfolder.delete force
                end if


          ' warning message if not working with root folder
    ''Add single quote before next line to supress pop-up message
          wscript.echo folder.path & " is not a root folder."

    end if

    set subfolder = Nothing
    set folder = Nothing
    Set fs = Nothing
    LVL 16

    Expert Comment


    Author Comment

    Thank you for your script.   I tried it and it appears to be what I need though I get an WSH Error dialogue box appear stating an error in:
    line: 27
    Char: 19
    Error: Permission Denied.

    I am not sure which folder this is as I can delete all from windows.  I also added everyone FC Share and NTFS to try and drop the permissions.  Not sure why this error occurs.

    Also does this only delete the Folder in the root?  I do no wish it to delete the subfolders of Folders it doesn't delete.

    I have used that application in the past and hadn't thought of using it for this task.  I'll look into it.

    The xcopy /d requires a specified date, if I read that correct.  I'd rather not have to tweak the specific date each week.  Now the complex code.... I'd rather no edit the VB.  My strength is in Network Infrastructure and Servers, sadly not scripting.

    Thanks for all your contributions.

    Accepted Solution

    I tested my original script on a very simple folder structure with no problems.  When I took a copy of a Windows user profile under \Documents and Settings..., I was able to reproduce the error.  Please try substituting line 27 with new code that worked for me...

    old line:
          subfolder.delete force

    new line:
         fs.deletefolder subfolder.path, True

    Regarding your other question, "Also does this only delete the Folder in the root?  I do no wish it to delete the subfolders of Folders it doesn't delete."...

    Please refer to the following directory structure:
         Dir1 (61 days old)
              SubDir1 (365 days old)
         Dir2 (10 days old)
              SubDir2 (365 days old)

    The script will only look at Dir1 and Dir2 (root directories off of the G: drive) and compare their respective ages.  It completely ignores SubDir1 and SubDir2.  It doesn't even care if they exist or how old they are.  In the example above, Dir1 meets your criteria for deletion and Dir1 and all of it's contents including SubDir1 will be deleted.  Dir2 and all of it's contents will be left alone.

    Let me know how the code substitution goes to get rid of the permission denied pop-up.

    Author Closing Comment

    YES it works and works well.  Thank you.  This script will be put to good use.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT ( The crux of this batch f…
    I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now