Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 630
  • Last Modified:

VB Script to delete all files older than 3 years

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 :-)
0
churchlandsshs
Asked:
churchlandsshs
  • 2
1 Solution
 
sirbountyCommented:
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
Next
 
objOut.Close

Open in new window

0
 
crokeefe28Commented:
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 
        Next 
    End If 
        objFSO.CreateTextFile(cLOG,True).Write(strLOG) 
    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 
    Next 
   '* 
   '*  Get Subfolders 
   '* 
    For Each strFOL In objFSO.GetFolder(folder).SubFolders 
        Call Get_Files(strFOL.Path) 
    Next 
End Sub

Open in new window

0
 
sirbountyCommented:
Thanx for the grade! :^)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now