Delete Folders & Files based on date last modified

Desmondo

Board Regular
Joined
Feb 27, 2013
Messages
70
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.

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.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top