IIS日志删除脚本

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
WScript.Echo \"\"
if (FailedToDelete > 0) then
  WScript.Echo \"There were \" & FailedToDelete  & \" files that could not be deleted.\"
end if
WScript.Echo \"There were \" & Deleted & \" files deleted.\"
end function
Sub DisplayHelpMessage()
   WScript.Echo
   WScript.Echo \"Usage:\"
   WScript.Echo \"      DeleteOldWebSiteLogfiles.VBS MaxDays WebSiteNumber \"
   WScript.Echo
   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
   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
   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
   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,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值