vbs script to delete files and folders that are older then 24hrs

Posted on 2011-04-18
Last Modified: 2012-05-11
hi all

im looking for a script that will delete old files and folders from some temp locations we have and a couple of other locations.

i have found the code below on this site, which is great for deleting files older then 24hrs.
but it doesnt remove the folders.

my vbs scripting knowledge is very poor so i have no idea how to modify this script so it removes the old folders as well.

or does anybody have a script that i can run after this one that will remove the folders as well??


strRoot = "c:\VMLocal\backup"
strMask = "*.*"
blnSubFolders = True
intHours = 24
strCommand = "cmd /c @echo off & dir " & Chr(34) & _
    strRoot & "\" & strMask & Chr(34) & " /a:-d /b"
If blnSubFolders Then
    strCommand = strCommand & " /s"
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec(strCommand)
Set objStdOut = objWshScriptExec.StdOut
Do Until objStdOut.AtEndOfStream
    strFile = objStdOut.ReadLine
    If Not blnSubFolders Then
        strFile = strRoot & "\" & strFile
    End If
    Set objFile = objFSO.GetFile(strFile)
    If DateDiff("h", objFile.DateLastModified, Now) > intHours Then
        WScript.Echo "Delete " & strFile
        'objFile.Delete True
    End If

Open in new window

Question by:BakerSyd
    LVL 12

    Accepted Solution


    Try the following code

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objStartFolder = "c:\VMLocal\backup"
    on error resume next
    Set objFolder = objFSO.GetFolder(objStartFolder)
    WScript.Echo objFolder.Path
    Set colFiles = objFolder.Files
    For Each objFile In colFiles
        If DateDiff("h", objFile.DateLastModified, Now) > 24 Then
            WScript.Echo objFile.Name & " Delete"
            objFile.Delete True
        End If
    ShowSubfolders objFSO.GetFolder(objStartFolder)
    Sub ShowSubFolders(Folder)
        For Each Subfolder In Folder.SubFolders
            WScript.Echo Subfolder.Path
            Set objFolder = objFSO.GetFolder(Subfolder.Path)
            If DateDiff("h", objFolder.DateLastModified, Now) > 24 Then
                WScript.Echo objFolder.Name & " Delete"
                objFolder.Delete True
                Set colFiles = objFolder.Files
                For Each objFile In colFiles
                    If DateDiff("h", objFile.DateLastModified, Now) > 24 Then
                        WScript.Echo objFile.Name & " Delete"
                        objFile.Delete True
                    End If
            End If
    End Sub

    Open in new window


    Author Closing Comment

    excellent script, does exactly what i need


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
    Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
    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…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now