I have an archive directory which is used to store files for each case that i work on. Each case has a Folder with Name and ref attached and several files inside word and xls.
I have a created a tool which generates the letters / Forms and saves them in the requisite folder for each case. I want to create a Purge button which deletes the whole case folder after 2 days from Last modified date. The code i am using is below and i get that the objfolder is empty which it is not.
Would really appreciate help on this, everything i try on the net does not seem to work.
I have a created a tool which generates the letters / Forms and saves them in the requisite folder for each case. I want to create a Purge button which deletes the whole case folder after 2 days from Last modified date. The code i am using is below and i get that the objfolder is empty which it is not.
Code:
Private Sub CommandButton8_Click()'On Error Resume Next ' <-- Comment out for troubleshooting.
strPath = ThisWorkbook.Path & "\" & "Archive" & "\" ' Update this parent folder, but don't screw it up.
MsgBox strPath
' PURPOSE: This script will delete all files recursively starting from
' strPath older than 2 days and empty subfolders.
' Very convenient for cleaning temp and log files.
Dim objFolder As Folder
Dim objSubFolder As Folder
Dim objFile As File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)
Call Search(objFolder)
End Sub
Code:
Sub Search(objFolder) For Each objFile In objFolder.Files
If DateDiff("d", objFile.DateLastModified, Now) > 2 Then objFile.Delete
Next
For Each objSubFolder In objFolder.SubFolders
Search (objSubFolder)
Next
For Each objSubFolder In objFolder.SubFolders
If objSubFolder.Files.Count = 0 Then objSubFolder.Delete
Next
End Sub
Would really appreciate help on this, everything i try on the net does not seem to work.