We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


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

BakerSyd asked
Medium Priority
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

Watch Question

Top Expert 2011
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview


excellent script, does exactly what i need

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.