VB Script to delete all files older than 3 years

Posted on 2009-02-23
Last Modified: 2012-05-06
I need a VB Script which goes through a defined directory and deletes all files older than 3 years. If possible I would also like a log to be generated of all files deleted.

Any help much appreciated :-)
Question by:churchlandsshs
    LVL 67

    Accepted Solution

    Run on a test folder first...just to be sure...
    Const TheFolder="C:\MyFolder\"
    Const Output = "C:\DeleteReport.txt"
    DateToDelete = DateAdd("yyyy",-3,Now())
    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objFolder : Set objFolder = objFSO.GetFolder(TheFolder)
    Dim objOut : Set objOut = objFSO.CreateTextFile (Replace(Output, ".txt", "_" & Year(Date) & Month(Date) & Day(Date) & ".txt"))
    msgbox DateToDelete
    For Each file in objFolder.Files
      If file.DateCreated < DateToDelete Then  'Can also test against file.DateLastModified (or DateLastAccessed)
        objOut.WriteLine "Deleting " & file
        objFSO.DeleteFile file, True
      End If

    Open in new window

    LVL 7

    Expert Comment

    This should do it, just customize it for yourself
    Option Explicit 
    '*  Delete all files with the extension "cEXT" in 
    '*  folders under "cFOL" older than "cDAZ" days. 
       '*  Declare Constants 
        Const cVBS = "DeleteOldTmpFiles.vbs" 
        Const cLOG = "DeleteOldTmpFiles.log" 
        Const cFOL = "C:\windows\temp" 
        Const cDAZ = 90 
        Const cEXT = ".log" 
       '*  Declare Variables 
        Dim strDAT 
        Dim intDAZ 
        Dim arrFIL() 
      ReDim arrFIL(0) 
        Dim intFIL 
            intFIL = 0 
        Dim strFIL 
        Dim strFOL 
        Dim strLOG 
        Dim strMSG 
            strMSG = " " & cEXT & " files deleted under " & cFOL 
        Dim dtmNOW 
            dtmNOW = Now 
       '*  Declare Objects 
        Dim objFSO 
       '*  Delete_Files() 
        MsgBox Delete_Files(cFOL) & strMSG,vbInformation,cVBS 
    Function Delete_Files(folder) 
        Delete_Files = 0 
       '*  Process folder 
        Set objFSO = CreateObject("Scripting.FileSystemObject") 
        If objFSO.FolderExists(folder) Then 
            Call Get_Files(folder) 
           '*  Delete Files 
            For intFIL = 1 To UBound(arrFIL) 
                strFIL = arrFIL(intFIL) 
                objFSO.DeleteFile strFIL, True 
                strLOG = strLOG & strFIL & vbCrLf 
        End If 
        Set objFSO = Nothing 
       '*  Return Results 
        Delete_Files = UBound(arrFIL) 
    End Function 
    Sub Get_Files(folder) 
       '*  Get Files 
        For Each strFIL In objFSO.GetFolder(folder).Files 
            If LCase(Right(strFIL.Name,Len(cEXT))) = cEXT Then 
                strDAT = strFIL.DateLastModified 
                intDAZ = DateDiff("d",strDAT,dtmNOW)
                If intDAZ > cDAZ Then 
                    intFIL = intFIL + 1 
                    ReDim Preserve arrFIL(intFIL) 
                    arrFIL(intFIL) = folder & "\" & strFIL.Name 
                End If 
            End If 
       '*  Get Subfolders 
        For Each strFOL In objFSO.GetFolder(folder).SubFolders 
            Call Get_Files(strFOL.Path) 
    End Sub

    Open in new window

    LVL 67

    Expert Comment

    Thanx for the grade! :^)

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (…
    Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    733 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

    21 Experts available now in Live!

    Get 1:1 Help Now