Dateisystem
Private Sub deleteOldLogFiles(ByVal logFileStorageDays As Integer)
'alle LogfileInfos holen
Dim logFiles() As IO.FileInfo
Try
'Zugriff auf das Verzeichnis
Dim LogFile As New IO.FileInfo(myLogFilePath & myLogFileName)
logFiles = LogFile.Directory.GetFiles("*" & myLogFileExtension, IO.SearchOption.TopDirectoryOnly)
Catch ex As Exception
MsgBox ("Path not found." & ex.Message)
Exit Sub
End Try
Dim aktCount As Integer = logFiles.Length
Dim x As Integer 'Hilfsindex
Dim fN As IO.FileInfo 'zu löschende Datei
'suche, solange die existierende Anzahl größer als erlaubt ist
Do While aktCount > logFileStorageDays
fN = Nothing
Dim oldest As DateTime = Date.MaxValue
For Each i As IO.FileInfo In logFiles
If Not i Is Nothing Then
If i.CreationTime < oldest Then
fN = i 'ist derzeit die älteste
oldest = i.CreationTime
End If
End If
Next
'If fN Is Nothing Then Exit Sub 'sollte nicht passieren
fN.Delete() 'lösche die älteste Datei
aktCount -= 1
For Each i As IO.FileInfo In logFiles
If Not i Is Nothing Then
If fN.CreationTime = i.CreationTime Then
x = Array.IndexOf(logFiles, fN)
logFiles(x) = Nothing 'auch aus dem Array nehmen
Exit For
End If
End If
Next
Loop
End Sub
Labels: Datei, Dateisystem, Löschen, Ordner, Zählen


0 Kommentare:
Kommentar veröffentlichen
Links zu diesem Post:
Link erstellen
<< Startseite