J
John J.
I've written the function below which deletes backupfiles (of which the name
starts with yearmonthday) older than 30 days in a specified folder. Though
it works, I'm not sure about the err_handler. The handler takes care of the
situation where Cdate() doesn't give a valid date and types don't match.
I would be happy if someone could give me advice if and how this could be
done better.
Thank you,
John
Function delBU(strBUfolder As String)
Dim strTemp As String
Dim d As Date
On Error GoTo Err_Handler
strTemp = Dir(strBUfolder)
Do While strTemp <> vbNullString
d = CDate(Mid(strTemp, 5, 2) & "/" & Mid(strTemp, 7, 2) & "/" &
Left(strTemp, 4)) 'month/day/year
If Date - d > 30 Then
Kill strBUfolder & strTemp
End If
strTemp = Dir
Loop
Exit_Handler:
Exit Function
Err_Handler:
If Err.Number = 13 Then 'Types do not match
d = Date 'Set to today so that this file doesn't get deleted
Resume Next
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
Resume Exit_Handler
End Function
starts with yearmonthday) older than 30 days in a specified folder. Though
it works, I'm not sure about the err_handler. The handler takes care of the
situation where Cdate() doesn't give a valid date and types don't match.
I would be happy if someone could give me advice if and how this could be
done better.
Thank you,
John
Function delBU(strBUfolder As String)
Dim strTemp As String
Dim d As Date
On Error GoTo Err_Handler
strTemp = Dir(strBUfolder)
Do While strTemp <> vbNullString
d = CDate(Mid(strTemp, 5, 2) & "/" & Mid(strTemp, 7, 2) & "/" &
Left(strTemp, 4)) 'month/day/year
If Date - d > 30 Then
Kill strBUfolder & strTemp
End If
strTemp = Dir
Loop
Exit_Handler:
Exit Function
Err_Handler:
If Err.Number = 13 Then 'Types do not match
d = Date 'Set to today so that this file doesn't get deleted
Resume Next
Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End If
Resume Exit_Handler
End Function