Here you go! You can thank some dude over at Ozgrid. Was posted in Jan 2012. Just replace the path and how many days old the files you want to delete. It will delete ALL files in that folder that are too old.
On a side note... can someone please tell me what that little circle is before "FSO" in the second code? I've never seen those before.
AND..
On a side note... can someone please tell me what that little circle is before "FSO" in the second code? I've never seen those before.
Code:
Sub DeleteOldFiles()
Set FSO = CreateObject("Scripting.FileSystemObject")
For Each fcount In FSO.GetFolder(ThisWorkbook.Path & "\Backups\ ").Files
If DateDiff("d", fcount.DateCreated, Now()) >7 Then
Kill fcount
End If
Next fcount
End Sub
AND..
Code:
Sub DelOldFiles()
''Clear out all files over 7 days old from Dir_Path folder.
'
Dir_Path = "C:\Folder\SubFolder\"
iMaxAge = 7 ' Set the number of days
Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FolderExists(Dir_Path) Then 'Check that the folder exists
For Each oFile In oFSO.GetFolder(Dir_Path).Files
If DateDiff("d", oFile.DateLastModified, Now) > iMaxAge Then 'Look at each file to check if it is older than 7 days
oFile.Delete
End If
Next
End If
End Sub