之前在收到IIS 服务器(monitor with zabbix)的异常邮件时,发现HDD的容量所剩无几,打开相关目录,发现IIS的log日志达到4G,随联想到为何不做一个批处理,然后执行计划任务去定期清理,所以就有了下面的程序。
一:prepare
要实现这样的需求,先要找到log日志的所在路径Log_path,然后清除Log_path下的所有文件(要注意权限的问题,在设置计划任务的时候)
当然,有时候可能会用到这些日志,所以也可以按照日志的生成日期,move到其他磁盘(包括Map一个镜像到本地的L:with dos net use)
solution 1>.
运用DOS命令,ClearIISLog.bat 代码如下
1 @echo off
2
3 echo ----- Clear IIS6.0 log file-----
4 echo ----- OS:Windows server 2008 ,Machine Name:dn2test04------
5 REM ----- Every week clear log file, Time: Sun 8:00------
6
7 for /f "tokens=1,2,3,4 delims=/ " %%a in ("%date%") do set mt=%%a&set d=%%b&set yr=%%c
8
9 for /f "tokens=1,2,3,4 delims=. " %%a in ("%time%") do set t=%%a
10 REM echo %t%
11 echo [%mt%/%d%/%yr% %fl% %t%] Starting -Clear iis log file >> "C:\Windows\Temp\IISClear%yr%%mt%%d%.log"
12
13 cd C:\inetpub\logs\LogFiles\W3SVC1\
14 dir >> "C:\Windows\Temp\IISClear%yr%%mt%%d%.log"
15
16 set logfile=C:\inetpub\logs\LogFiles\W3SVC1\*.log
17 del %logfile%
18
19 for /f "tokens=1,2,3,4 delims=. " %%a in ("%time%") do set e=%%a
20 REM echo %e%
21 echo [%mt%/%d%/%yr% %fl% %e%] End - Clear iis log file >> "C:\Windows\Temp\IISClear%yr%%mt%%d%.log"
solution 2 >.
VBScript. ClearIISLog.vbs, 代码如下:
1 'path
2 'ext
3 'expiredDays ±
4
5 Sub LogCleaner(path,ext,expiredDays)
6
7 On Error Resume Next
8
9 Set oFso = CreateObject("Scripting.FileSystemObject")
10
11 If Not oFso.FolderExists(path) Then Exit Sub 'Ŀ¼²»´æÔÚÍ˳ö
12
13 Set oFolder = oFso.GetFolder(path)
14
15 For Each oFile In oFolder.Files '´¦ÀíĿ¼ÏÂËùÓÐÎļþ
16
17 'ÅжÏÎļþÀàÐͼ°×îºóÐÞ¸ÄÈÕÆÚ
18 If right(oFile.name,3)=ext And DateDiff("d",oFile.DateLastModified,Date) > expiredDays Then oFile.delete
19
20 Next
21
22 For Each oSubFolder In oFolder.SubFolders 'µÝ¹é´¦Àí×ÓÎļþ¼Ð
23
24 LogCleaner oSubFolder.Path,ext,expiredDays
25
26 Next
27
28
29 End Sub
30
31 'ɾ³ýÒ»¸öÔÂ֮ǰµÄIISÈÕÖ¾
32 LogCleaner "C:/WINDOWS/system32/LogFiles/W3SVC1","log",90
第2种办法相对来讲,更灵活,第1种,简单易懂。