Pages

Friday, 18 September 2009

Delete Old log files using VB6

VB6 Snippet to delete old log files

Private Sub deleteOldFiles()

Dim sFileName As String
Dim sFileSplit() As String
Dim sFileSpec As String
Dim sDir As String
Dim iCount As Integer
Dim iCtr As Integer
Dim dCompDate As Date
Dim dFileDate As Date

On Error GoTo errHandler

sFileSpec = App.Path & "\logs\*.log"
sFileName = Dir(sFileSpec)

dCompDate = Format(Now, "mm/dd/yyyy")

Do
    If sFileName = "" Then Exit Do
        If InStr(sFileSpec, "\") > 0 Then
            sFileSplit = Split(sFileSpec, "\")
            iCount = UBound(sFileSplit) - 1
                For iCtr = 0 To iCount
                    sDir = sDir & sFileSplit(iCtr) & "\"
                Next
            sFileName = sDir & sFileName
        End If
        dFileDate = Format(FileDateTime(sFileName), "mm/dd/yyyy")
        If DateDiff("d", dFileDate, dCompDate) >= 3 Then
         'Get File Attributes
          If GetAttr(sFileName) = 33 Then
             SetAttr sFileName, 32
          End If
         Kill sFileName
        End If
    sFileName = Dir
    sDir = ""
Loop

Exit Sub

errHandler:

MsgBox Err.Number & ":" & Err.Description

End Sub

No comments:

Post a Comment