1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| Option Explicit
Dim strMessageRetour
''''''''''
' Fonction ErrorOccurred () : Indique si il y a eu une erreur ou non
Function ErrorOccurred()
ErrorOccurred = False
If Err.Number <> 0 Then
ErrorOccurred = True
End If
End Function
'''''
''''''''''''''''''''
' Fonction Info() :
Sub Info(strRetour)
' Journalistation du script : Incrémente la str de compte rendu du déroulement du script
'WScript.Echo strRetour
strMessageRetour = strMessageRetour & vbCrLf & vbCrLf & strRetour
End Sub
'''''
''''''''''
' Fonction LogRotate () : Efface les anciens fichiers de log de type YYYYMMDD_*. Exemple de fichier de log : 20091127
' intNumberOfDays --> Durée de sauvegarde des logs, en nombre de jours. Exemple : 30
' strFolderPath --> Chemin du répertoire de Log. Exemple : "C:\Log\"
' strLogFilesExtensions --> Extentions des fichiers de log à mettre dans le cadre du LogRotate. Exemple : ".log"
' Si il y a plusieurs extensions, les séparer par des virgules. Exemple : ".log, .txt, .csv"
' Pour prendre en comtpe toutes les extensions, mettre un espace. Exemple : " "
Function LogRotate(intNumberOfDays, strFolderPath, strLogFilesExtensions)
On Error Resume Next
Dim objFso, objLogFolder, objLogFile
Dim dateNow, dateLogFileCreation
Dim strLogFileDate, strExtension
Dim boolErrorOccurred
Dim intDeletedFiles
Set objFso = CreateObject("Scripting.FileSystemObject")
dateNow = Now
boolErrorOccurred = False
intDeletedFiles = 0
' Si le dossier de log n'existe pas
If NOT objFso.FolderExists(strFolderPath) Then
LogRotate = "Le dossier strFolderPath n'existe pas"
Exit Function
End If
Info "LogRotate sur les fichiers de plus de " & intNumberOfDays & " jours dans le répertoire " & strFolderPath & " pour les fichiers d'extensions """ & strLogFilesExtensions & """"
Set objLogFolder = objFso.GetFolder(strFolderPath)
For Each objLogFile In objLogFolder.Files
' Pour chaque fichier du répertoire de log, si l'extension correspond, que le fichier est au bon format
' et que la date du fichier est antérieure à la date limite de LogRotate, alors on le supprime
For Each strExtension In Split(strLogFilesExtensions, ",")
If UCase(Right(objLogFile.Name, Len(Trim(strExtension)))) = UCase(Trim(strExtension)) Then
strLogFileDate = Left(objLogFile.Name,9)
If Len(strLogFileDate) = 9 AND Right(strLogFileDate, 1) = "_" AND Left(strLogFileDate, 1) = "2" Then
strLogFileDate = Left(strLogFileDate, 8)
dateLogFileCreation = DateValue(Left(strLogFileDate, 4) & "/" & Left(Right(strLogFileDate, 4), 2) & "/" & Right(strLogFileDate, 2))
If ErrorOccurred Then
LogRotate = "Les formats de fichier de Log ne sont pas au bon format"
Exit Function
End If
' Test de la date du fichier par rapport à la date limite de LogRotate
If DateDiff("d", DateAdd("d", - intNumberOfDays, dateNow), dateLogFileCreation) < 0 Then
Info vbtab & "Suppression du fichier " & objLogFile.Name
objLogFile.Delete
If ErrorOccurred Then
Info vbtab & vbtab & "ERREUR : Impossible de supprimer ce fichier"
boolErrorOccurred = True
Else
Info vbtab & vbtab & "OK"
intDeletedFiles = intDeletedFiles + 1
Exit For
End If
Else
Info vbtab & "Fichier " & objLogFile.Name & " trop récent pour être supprimé"
End If
End If
End If
Next
Next
Info "Nombre de fichiers de log supprimés : " & intDeletedFiles
If boolErrorOccurred = True Then
LogRotate = "Erreurs lors du LogRotate"
Else
LogRotate = True
End If
Set objLogFile = Nothing
Set objLogFolder = Nothing
Set objFso = Nothing
End Function
'''''
strMessageRetour = ""
WSCript.Echo LogRotate(30, "C:\test2\", "log") & vbCrLf & vbCrLf & strMessageRetour
strMessageRetour = ""
WSCript.Echo LogRotate(30, "C:\test1", " ") & vbCrLf & vbCrLf & strMessageRetour
strMessageRetour = ""
WSCript.Echo LogRotate(30, "C:\test3\", "ldif,csv,txt") & vbCrLf & vbCrLf & strMessageRetour |