گروه فناوری پرند - Parand Tech Group

How to Delete Log Files

Can't remember where I found the script, but if you are a SysAdmin and looking to do a Log Rotation deleting the old ones, this would help you:

Option Explicit

Dim intDaysOld, strObjTopFolderPath, strLogFIleSuffix, ObjFS, ObjTopFolder
Dim ObjDomainFolder, ObjW3SvcFolder, ObjSubFolder, ObjLogFile, ObjFile

intDaysOld   = 1
strObjTopFolderPath = "x:\TopFolder"
strLogFIleSuffix = ".log"
'Assumes folder structure is like the following:
' x:\TopFolder\
'              whatever.com\
'                           w3svcX\
'                                  ex020101.log

Set ObjFS = CreateObject("Scripting.FileSystemObject")
Set ObjTopFolder = ObjFS.GetFolder(strObjTopFolderPath)

For Each ObjDomainFolder in ObjTopFolder.SubFolders
 For Each ObjW3SvcFolder in ObjDomainFolder.SubFolders
  Set ObjSubFolder = ObjFS.GetFolder(ObjW3SvcFolder)
   For each ObjLogFile in ObjSubFolder.files
    Set ObjFile = ObjFS.GetFile(ObjLogFile)
    If datediff("d",ObjFile.DateLastModified,Date()) > intDaysOld and lcase(right(ObjLogFile,4))=strLogFIleSuffix then
     '*****************************************************
     'DON'T UNCOMMENT THIS UNTIL YOU KNOW IT WORKS PROPERLY!!!
     WScript.Echo("Will delete " & ObjSubFolder.name & "\" & ObjFile.name)
     'ObjFile.Delete
     '*****************************************************
    End If
    Set ObjFile = nothing
   Next
  Set ObjSubFolder = nothing
 Next
Next

Set ObjTopFolder = nothing
Set ObjFS = nothing

Print | posted on Friday, September 07, 2007 7:50 AM

Comments on this post

No comments posted yet.

Your comment:

 (will show your gravatar)
 
Please add 3 and 6 and type the answer here: