Delete files within a folder

T

tarzan538

I have within a folder a report worksheet and a macro worksheet tha
create reports on a daily based. Therefore this folder has tons o
reports and I need to add some code to the macro worksheet to delet
reports worksheets that are older that 90 days. This is the code that
believe could do the job; Do you have any suggestions or maybe
different approach?

Dim Filename As String, RecentFile As String, RecentDate As Date
Dim DelFile As String, Directory As String, myDir As String, OldFile A
String

Sub Delete90DaysOldFiles()

myDir = "c:\data\excel\reports"
If Right(myDir, 1) <> "\" Then Directory = myDir & "\"
Filename = Dir(Directory & "" & "*.xls", 0)
Do While Filename <> ""
RecentFile = Filename
RecentDate = FileDateTime(Directory & Filename)
If FileDateTime(Directory & Filename) < Date - 90 Then '90 days old
OldFile = Filename
End If
Filename = Dir
If OldFile <> "" Then
Kill OldFile
End If
Loop

End Su
 
T

Tom Ogilvy

Sub Delete90DaysOldFiles()

Dim Filename As String, myDir As String, OldFile As String
Dim DelFile As String, Directory As String,

myDir = "c:\data\excel\reports"
If Right(myDir, 1) <> "\" Then Directory = myDir & "\"
Filename = Dir(Directory & "" & "*.xls", 0)
Do While Filename <> ""
' next two lines not used
'RecentFile = Filename
'RecentDate = FileDateTime(Directory & Filename)
If FileDateTime(Directory & Filename) < Date - 90 Then '90 days old
OldFile = Filename
End If
Filename = Dir
If OldFile <> "" Then
Kill OldFile
End If
' reinitialize OldFile
OldFile = ""
Loop

End Sub

Since you are changing the directory within a DIR loop, you could have
problems, but as long as you don't then it appears like it should work.


You might want to make a backup of your directory until you are sure it is
working correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top