How can I delete all files from my IIS log file directory that are over 90 days old?
這個腳本可以從日志目錄中删除90天的記錄。
Option Explicit
Const GENERAL_FAILURE = 2
Const KillFile=0 ' Set this to 0 to not delete the files, set to 1 to delete the files
Dim ArgObj, Servername, WebSiteID, WebSite, WebSitepath, totalDeleted, MaxAgeOfFileToKeep
Function DeleteOldLogFiles(WebSite, MaxAgeOfFile)
Dim File, ServerObj, FSO, FolderObj, FileObj, LogFileDir, Deleted, Status, FailedToDelete
Deleted = 0
FailedToDelete= 0
on error resume next
' Attempt to get the web site object from the metabase
Err.clear
Set ServerObj = GetObject(WebSite)
If (Err.Number <> 0) Then
WScript.Echo \"Error: \" & Err.Description & \" (\" & Err.Number & \")\"
Exit Function
end if
LogFileDir = ServerObj.LogFileDirectory
Set ServerObj = Nothing
WScript.Echo \"Log file dir for: \" &WebSite & \" = \" & LogFileDir
WScript.Echo \"Delete files over \"& MaxAgeOfFile & \" days old.\"
WScript.Echo \"\"
Set FSO = CreateObject(\"Scripting.FileSystemObject\")
set Folderobj = FSO.GetFolder(LogFileDir)
for each File in Folderobj.files
if (Date - File.DateCreated > cint(MaxAgeOfFile)) then
Status = \"Deleting File: \" & File.name & \", Age=\" & formatNumber(Date-File.DateCreated, 0) & \" days, Status=\"
Err.Clear
if (KillFile = 1) then
FSO.DeleteFile(LogFileDir & \"\" & File.Name)
If (Err.Number <> 0) Then
Status = Status & \"Failed : \"& Err.Description & \" (\" & Err.Number & \")\"
FailedToDelete = FailedToDelete +1
else
Status = Status & \"Deleted\"
Deleted = Deleted + 1
end if
else
Status = Status & \"Skipped\"
end if
WScript.Echo Status
end if
next
DeleteoldLogfiles = Deleted
if (FailedToDelete > 0) then
WScript.Echo \"There were \" & FailedToDelete & \" files that could not be deleted.\"
WScript.Echo \"There were \" & Deleted & \" files deleted.\"
end function
Sub DisplayHelpMessage()
WScript.Echo
WScript.Echo \"Usage:\"
WScript.Echo \" DeleteOldWebSiteLogfiles.VBS MaxDays WebSiteNumber \"
WScript.Echo \"MaxDays = maximum age in days of files to keep.\"
WScript.Echo \"WebSiteNumber is the number of the web site, you have two methods to determine this:\"
WScript.Echo \"#1 = Run FINDWEB.VBS\"
WScript.Echo \"#2 = Right click the web site, select properties, on the web site tab\"
WScript.Echo \" under logging click the Properties button, part of the log file\"
WScript.Echo \" name will contain the web site #\"
WScript.Echo \" example log filename: W3SVC1\exyymmdd.log - the web site is 1\"
WScript.Echo \" W3SVC45\exyymmdd.log - the web site is 45\"
WScript.Echo \"Please visit and support : http://www.iisfaq.com\"
end sub
' Get the Arguments object
Set ArgObj = WScript.Arguments
' Test to make sure there is at least one command line arg - the command
If ArgObj.Count < 2 Then
DisplayHelpMessage
WScript.Quit (GENERAL_FAILURE)
End If
Servername = \"LocalHost\"
MaxAgeOfFileToKeep = trim(ArgObj(0))
WebSiteID = trim(ArgObj(1))
WebSite = \"W3SVC/\" & WebSiteID
WebSitepath = \"IIS://\" & Servername &\"/\" & WebSite
TotalDeleted = DeleteOldLogFiles(WebSitePath, MaxAgeOfFileToKeep)
本文轉自 蘇繁 51CTO部落格,原文連結:http://blog.51cto.com/goxia/224809,如需轉載請自行聯系原作者