'IIS日志清理之IIS日志生成系统(CreateIISLog.vbs)
'创建文件夹
Function CreateFolder(Folder)
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CreateFolder(Folder)
If Err>0 Then
Err.Clear
CreateFolder = False
Else
CreateFolder = True
End If
End Function
'创建文件
Function CreateFile(FileName, Content)
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fd = FSO.CreateTextFile(FileName, True)
fd.WriteLine Content
If Err>0 Then
Err.Clear
CreateFile = False
Else
CreateFile = True
End If
End Function
CreateFolder "D:\IISLogTest"
CreateFolder "D:\IISLogTest\IISLogs001"
CreateFolder "D:\IISLogTest\IISLogs002"
CreateFolder "D:\IISLogTest\IISLogs003"
for i=1 to 30
d=date-i
filename="ex" & right(year(d),2) & right("0" & month(d),2) & right("0" & day(d),2) & ".log"
CreateFile "D:\IISLogTest\" & filename,Content
CreateFile "D:\IISLogTest\IISLogs001\" & filename,Content
CreateFile "D:\IISLogTest\IISLogs002\" & filename,Content
CreateFile "D:\IISLogTest\IISLogs003\" & filename,Content
next
'**********************************************************
<job id="IIS日志清理WSH版代码(DelIISLog.wsf)
<script language="vbscript">
'作者:柳永法(yongfa365)'Blog
'修改:2007-11-15
'操作说明:此文件只能清除一天的日志,得使用计划任务让其每天执行一次,因为服务器上一般都禁用WScript.Shell,所以不推荐使用
Function DelIISLog(IISLogPath,beforedays)
d=Now-beforedays
If Right(IISLogPath,1) <> "\" Then IISLogPath=IISLogPath & "\"
p= IISLogPath & "ex" & Right(Year(d),2) & Right("0" & Month(d),2) & Right("0" & Day(d),2) & ".Log"
Set WshShell = WScript.CreateObject("WScript.Shell")
wscript.echo p
WshShell.Run ("cmd.exe /c del /s " & p)
Set WshShell = Nothing
End Function
DelIISLog "D:\IISLogTest",2
</script>
</job>
'**********************************************************
'IIS日志清理VBS版代码(DelIISLog.vbs)
'调用方法:DelIISLog "IIS日志所在路径",保留多少天的IIS日志
'遍历IIS日志文件夹下的所有文件及子文件夹下的文件
Function DelIISLog(IISLogPath,KeepDays)
on error resume next
Set oFso = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFso.GetFolder(IISLogPath)
Set oSubFolders = oFolder.SubFolders '得到该目录下所有的文件夹的集合
Set oFiles = oFolder.Files '得到该目录下所有的文件的集合
'第一步处理当前目录下的所有文件
For Each oFile In oFiles '遍历所有文件
if right(oFile.name,3)="log" then
oDate=cdate("20" & mid(oFile.name,3,2) & "-" & mid(oFile.name,5,2) & "-" & mid(oFile.name,7,2))
if date-oDate > KeepDays then oFile.delete '判断是不是要处理的IIS日志文件,如果是的话直接删除
end if
Next
'第二步处理当前目录下的所有目录,进行递归调用
For Each oSubFolder In oSubFolders
DelIISLog oSubFolder.Path,KeepDays '递归
Next
End Function
DelIISLog "D:\IISLogTest",20 '遍历
'**********************************************************
//IIS日志清理JS版代码(DelIISLog.js)
//调用方法:DelIISLog("IIS日志所在路径",保留多少天的IIS日志);
//遍历IIS日志文件夹下的所有文件及子文件夹下的文件
function DelIISLog(IISLogPath,KeepDays){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFolder(IISLogPath);
var Folders = new Enumerator(f.SubFolders); //得到该目录下所有的文件夹的集合
var Files = new Enumerator(f.Files); //得到该目录下所有的文件的集合
//第一步处理当前目录下的所有文件
for (; !Files.atEnd(); Files.moveNext()) {
var fileName = Files.item().name;
var year = "20" + fileName.substr(2, 2);
var mouth = fileName.substr(4, 2);
var day = fileName.substr(6, 2);
var days = Math.round(((new Date()).getTime() - Date.UTC(year, mouth - 1, day)) / 1000 / 60 / 60 / 24);
if (days > KeepDays) Files.item().Delete(); //判断是不是要处理的IIS日志文件,如果是的话直接删除
}
//第二步处理当前目录下的所有目录,进行递归调用
for (; !Folders.atEnd(); Folders.moveNext()) {
DelIISLog(Folders.item(),KeepDays);
}
}
//调用函数,比如:"F:\\log",5 或 "C:\\windows\\system32\\LogFiles",5
DelIISLog("D:\\IISLogTest",2);
'**********************************************************
'IIS日志清理CMD版代码(DelIISLog.cmd)
@echo off
title clean log
::设置当前日期前多少天或后多少天
set/a beforedays=-3
::设置目录所在位置
set dir="F:\log\"
::当前日期转换为天数并进行计算
call :Date2Day %date:~0,10% days
set/a days=%days%%beforedays%
call :Day2Date %days% lastdate
::计算完毕,生成想要的字符组合
set okstr=ex%lastdate:~2,6%.log
::删除这些文件
del del /f /s /q %dir%\%okstr%
cmd /k
:Date2Day
setlocal ENABLEEXTENSIONS
for /f "tokens=1-3 delims=/-, " %%a in ('echo/%1') do (
set yy=%%a & set mm=%%b & set dd=%%c
)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
endlocal&set %2=%j%&goto :EOF
:Day2Date
setlocal ENABLEEXTENSIONS
set /a i=%1,a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
endlocal&set %2=%yy%%mm%%dd%&goto :EOF