IIS log cleanup of CMD edition VBS edition JS edition WSH edition

  • 2020-05-06 12:02:24
  • OfStack

Application: mainly used with virtual host, also can be used for personal server

Production background: on a certain day in 2005, has been running a normal virtual host crashed, let the room on duty staff restart several times, all failed, connect the monitor into the system to see, prompt: C disk space is insufficient, still have to go to the computer room in the middle of the night, into the room after the broken network first, and then found that there are two places into the system has a problem, C: \ WINDOWS \ system32 \ six G LogFiles files, another is the place where Symantec isolated virus, for the web, the greatest possibility is that the virtual host all logs are written here, and nobody knows what to write here, depressed, in IIS saw, also is really so, log in long everyday, at that time many people didn't pay attention to this order, then clean up, The system is normal. After returning to the company, I changed the IIS log to another disk.

The solution: this isn't the final solution, though. A web host can have hundreds of sites, and some sites can generate hundreds of M log files in a day and have to be cleaned up in a timely way.

And there are two solutions:
1. Clean
daily for the previous 60 days 2. Clean up your 60-day log later.
Unless you are a professional, you can find log files 60 days old to delete. However, even if you are very skilled, this method is very time-consuming. The best way is to use DOS batch processing or script implementation, the main scripts available are vbs and js.
In the solution below, there are several ways for you to choose the one that suits you. Their overall design idea is as follows:
The format of the IIS log file is: ex year, month and day.log such as: ex071116.log
IIS log file storage location: the default is in: %windir%\system32\LogFiles, if you use professional IIS management software, it will generally let you set the appropriate log directory
IIS log cleanup CMD edition: Calculated according to the current time N days before the date, such as today is: 2007-11-16, 60 days before the date is 2007-9-16 (program can automatically identify 30 days or embellish of 31 days or months), and then processed into 20070916 such format, and then assembled into ex070916. log IIS log file format, so we have to clean up the log file name then, We'll use del/s/f d: \ iislog \ ex070916 log to clear the log folder where all directories and subdirectories under the document on the file name, so, but this is just remove the log of a day, so we have to add the batch to the planning task, make it regularly every day, in this way, all the computer log we can keep the tube.
IIS log cleanup VBS edition: VBS edition is theoretically not as fast as iis edition, because it is also script-driven, unlike cmd edition which directly USES the batch processing function of dos system fast (guess), VBS after all is a high-level language, the ability to process dates in one sentence, while CMD edition has to write half a page. IIS log cleaning VBS version with the implementation of the VBS traversal IIS logs directory where all files, and folders, and then take variegated file name the date, then the current date - the date, and see if it is more than a set number of days, more than words delete, there is one thing about this line of thinking is a can remove N days ago all records, rather than just one day, he can clear CMD version log, you put the script into the planning tasks, run every day, can also be manually run over a period of time. This code is significantly less than the IIS log cleanup CMD version.
IIS log cleaning JS version: this version is no more and clear IIS log VBS edition, ideas are the same, just use scripting language is different, there is a call of each parameter in the two parameters: the directory, the directory to write: D: \ \ iislog, was when the main script in vbs before, mainly to learn C # this time, I heard that the two languages are about the same, just practice, also didn't take much time.
IIS log cleaning WSH version: WSH version is actually the most simple, because of his high degree of integration, operation process is as follows: using vbs or js generated to deal with the file name, and then use WScript. Shell cmd command, use the IIS CMD version and IIS log log cleaning VBS version of the advantages, this also is can only handle one day at a time log, of course you can also put it into a log of dealing with the many days. Because of WSH integration degree is high, you can perform many operations, so that hackers are like this, use most is WScript. Shell, so general security awareness higher server providers will give disabled this component, so that the function of the best becomes the most can't use, the generality of the worst.
Example code:
IIS log cleanup CMD version code (DelIISLog.cmd) :
 
@echo off 
title 
:: Sets how many days before or after the current date  
set/a beforedays=-3 
:: Set the location of the directory  
set dir="F:\log\" 
:: The current date is converted to days and calculated  
call :Date2Day %date:~0,10% days 
set/a days=%days%%beforedays% 
call :Day2Date %days% lastdate 
:: Is evaluated , Generate the desired character combination  
set okstr=ex%lastdate:~2,6%.log 
:: Delete these files  
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 

IIS log cleanup VBS (DelIISLog.vbs) :
 
'IIS Log cleaning VBS Version of the code (DelIISLog.vbs) 
' Call method: DelIISLog "IIS Log path ", For how many days IIS The log  
' traverse IIS All files under log folder and files under subfolder  
Function DelIISLog(IISLogPath,KeepDays) 
on error resume next 
Set oFso = CreateObject("Scripting.FileSystemObject") 
Set oFolder = oFso.GetFolder(IISLogPath) 
Set oSubFolders = oFolder.SubFolders ' Gets a collection of all the folders in the directory  
Set oFiles = oFolder.Files ' Gets the collection of all the files in the directory  
' The first step deals with all the files in the current directory  
For Each oFile In oFiles ' Iterate through all the files  
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 ' Decide if it's something to deal with IIS Log files, if so, are deleted  
end if 
Next 
' The second step processes all the directories in the current directory, making a recursive call  
For Each oSubFolder In oSubFolders 
DelIISLog oSubFolder.Path,KeepDays ' recursive  
Next 
End Function 
DelIISLog "D:\IISLogTest",20 ' traverse  

IIS log cleanup JS code (DelIISLog.js) :
 
//IIS Log cleaning JS Version of the code (DelIISLog.js) 
// Call method: DelIISLog("IIS Log path ", For how many days IIS The log ); 
// traverse IIS All files under log folder and files under subfolder  
function DelIISLog(IISLogPath,KeepDays){ 
var fso = new ActiveXObject("Scripting.FileSystemObject"); 
var f = fso.GetFolder(IISLogPath); 
var Folders = new Enumerator(f.SubFolders); // Gets a collection of all the folders in the directory  
var Files = new Enumerator(f.Files); // Gets the collection of all the files in the directory  
// The first step deals with all the files in the current directory  
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(); // Decide if it's something to deal with IIS Log files, if so, are deleted  
} 
// The second step processes all the directories in the current directory, making a recursive call  
for (; !Folders.atEnd(); Folders.moveNext()) { 
DelIISLog(Folders.item(),KeepDays); 
} 
} 
// Call a function, such as: "F:\\log",5  or  "C:\\windows\\system32\\LogFiles",5 
DelIISLog("D:\\IISLogTest",2); 

IIS log cleanup WSH version code (DelIISLog.wsf) :
 
<job id="IIS Log cleaning WSH Version of the code (DelIISLog.wsf)"> 
<script language="vbscript"> 
' Author: liu yongfa (yongfa365)'Blog 
' Modification: 2007-11-15 
' Action note: this file can only clear the log for one day, you have to use scheduled tasks to make it execute once a day, because it is generally disabled on the server WScript.Shell, So it's not recommended  
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> 

Sometimes when I get someone else's code, I have to write a bunch of things to test. Now if I ask you to test this, you won't test it directly on the server. Therefore, I will post the test script of my own liu yongfu, which mainly generates a test folder in D disk, and some IIS test log files,
IIS log cleaning IIS log generation system (CreateIISLog.vbs) :
 
'IIS Log cleaning IIS Log generation system (CreateIISLog.vbs) 
' Create folder  
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 
' Create a file  
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 


Postscript: this method can be used not only for IIS log processing, but also for Serv-U log processing, provided that the format of the Serv-U log file is also set to ex071115.log.

Related articles: