IIS log Cleanup (cmd version, VBS version, JS version, WSH version) _win server

Source: Internet
Author: User
Tags goto
Applications: Mainly used with virtual host, can also be used for personal server

Background: 2005 a month, the normal operation of the virtual host crashed, let the computer room on duty to restart a number of times, are not, the display into the system to see, tips: C disk space is insufficient, the middle of the night to go to the room to deal with, to the computer room after the first break network, and then into the system found two places have problems, C:\WINDOWS\ System32\logfiles file has 6G, there is a Symantec isolate the virus, to find the Internet, the greatest possibility is that all of our virtual host log is written here, and no one knows to write here, depressed, in the IIS looked at the next, is really such a thing, Log every day in the long, when the company orders a lot also no one to pay attention to this, then cleaned up a bit, the system is normal, back to the company after the IIS log changed to another disk.

Solution: But this is not the final Solution Ah, a virtual host hundreds of sites, and some sites can produce a day hundreds of m of log files, but also to clean up in time.

And there are two solutions:
1.60 days before the daily cleaning of the log
2. Over time, clean up the 60 days before the log.
But either way, it has to be handled by technology, manual to delete unless you are very professional, you can find 60 days before the log file to delete, but even if you are very good technology, this method is very time-consuming, the best way is: Using a DOS batch or script to implement, you can use the script is mainly VBS and JS.
There are several methods in the following solution that you can choose for yourself, and their overall design mentality is this:
The IIS log file is in the form of ex-month-day. Log, such as: Ex071116.log
IIS log file storage location:%windir%\system32\logfiles By default, if you are using a professional IIS management software, it will normally allow you to set up the appropriate log directory
IIS Log cleanup cmd version: According to the current time calculated the first n days of the date, such as today is: 2007-11-16, the first 60 days of the date is 2007-9-16 (the program can automatically identify 30 days or 31 days or run month), and then processed into 20070916 such a format, Then combine the IIS log file formats such as Ex070916.log so that we get the log file names to clean up and then we'll use del/s/f D:\iislog\ex070916.log To clear all files of this file name in the folder directory and subdirectories of the log. To clear the log, but this is only a day to clear the logs, so we have to add this batch to the scheduled task, so that it executes every day, so that all the computer log problems we can not control.
IIS Log cleanup VBS version: The VBS theory is that there is no IIS version fast, because he also need to use script-driven, and unlike the CMD version directly using the DOS System batch processing function quickly (guess), VBS is a high-level language, processing date ability with a word on the implementation, and the CMD version to write half a page. IIS log cleanup The implementation of the version of the VBS uses VBS to traverse all files and folders in the directory where the IIS logs reside, and then to combine the file names into the date type, and then the current date-this date to see if it is more than the set number of days, more than the words Delete, This idea has the advantage of being able to clear all the records of N days before, instead of just one day, he can you like the cmd version of the log cleanup, the script to write to the scheduled task, run every day, you can also run the time manually. This code is significantly less than the IIS log cleanup cmd version.
IIS Log Cleanup JS version: This version in fact, and IIS log clean up the VBS version is not much, the idea is the same, just use the scripting language is not the same, there is the call when the two parameters in each of the parameters: directory, this directory must be written: D:\\iislog, Used to use VBS as the main script, this is mainly to learn C #, I heard that both languages are similar, just also practice, also did not spend much time.
IIS Log Cleanup WSH version: WSH version is actually the simplest, because of his high degree of integration, the operating process is this: use VBS or JS to generate the file name to be processed, Then use the Wscript.Shell execution cmd command to handle, using the IIS log clean up the cmd version and IIS log clean up the VBS version of the advantages, this is only one day to process the log, of course, you can also change it to deal with many days of the log. Because of the high degree of WSH integration, you can do a lot of operations, so hackers are very fond of this, The most used is the Wscript.Shell, so the general security awareness of the higher server provider will be to disable this component, so that the best use of the function becomes the most can not be used, the least common.
Instance code:
IIS Log cleanup cmd code (DELIISLOG.CMD):
Copy Code code as follows:

@echo off
Title
:: Set how many days or days before the current date
set/a beforedays=-3
:: Set the location of the directory
Set dir= "F:\log\"
:: Convert current date to days and calculate
Call:D Ate2day%date:~0,10%
set/a days=%days%%beforedays%
Call:D ay2date%days% lastdate
:: Finished calculation, generate desired character combination
Set Okstr=ex%lastdate:~2,6%.log
:: Deleting these files
Del del/f/s/q%dir%\%okstr%
cmd/k
:D Ate2day
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
:D ay2date
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 set mm=0%mm%) & (if%dd% LSS set dd=0%dd%)
Endlocal&set%2=%yy%%mm%%dd%&goto:eof

IIS log cleans up the VBS version code (DELIISLOG.VBS):
Copy Code code as follows:

' IIS log cleans up the VBS version code (DELIISLOG.VBS)
' Call method: Deliislog IIS Log on path ', how many days of IIS log is retained
' Traverse all files under the IIS log folder and the files under subfolders
Function Deliislog (iislogpath,keepdays)
On Error Resume Next
Set oFSO = CreateObject ("Scripting.FileSystemObject")
Set ofolder = Ofso.getfolder (Iislogpath)
Set osubfolders = Ofolder.subfolders ' Get a collection of all folders in this directory
Set ofiles = Ofolder.files ' Gets a collection of all the files in the directory
' The first step is to handle all the files in the current directory
For every ofile in Ofiles ' Traverse all Files
If Right (ofile.name,3) = "Log" Then
Odate=cdate ("& Mid (ofile.name,3,2) &-" & Mid (ofile.name,5,2) & "-" & Mid (ofile.name,7,2))
If date-odate > Keepdays then ofile.delete ' Judge is not the IIS log file to be processed, if it is then directly deleted
End If
Next
' Second step handles all directories under the current directory and makes recursive calls
For each osubfolder in Osubfolders
Deliislog Osubfolder.path,keepdays ' recursion
Next
End Function
Deliislog "D:\IISLogTest", 20 ' traversal

IIS Log Cleanup JS version code (deliislog.js):
Copy Code code as follows:

IIS Log Cleanup JS version code (deliislog.js)
Call Method: Deliislog (IIS Log in path), how many days of IIS log is reserved;
Traverse all files under the IIS log folder and files under subfolders
function Deliislog (iislogpath,keepdays) {
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var f = fso. GetFolder (Iislogpath);
var Folders = new Enumerator (f.subfolders); Get a collection of all the folders in this directory
var Files = new Enumerator (f.files); Get a collection of all the files in this directory
The first step is to handle all the files in the current directory
for (;! Files.atend (); Files.movenext ()) {
var fileName = Files.item (). Name;
var year = "+" + 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 (); Determine if the IIS log files are being processed and if so delete directly
}
The second step is to handle all directories under the current directory and make recursive calls
for (;! Folders.atend (); Folders.movenext ()) {
Deliislog (Folders.item (), keepdays);
}
}
Call functions, such as: "F:\\log", 5 or "C:\\windows\\system32\\logfiles", 5
Deliislog ("D:\\iislogtest", 2);

IIS Log cleanup WSH version code (DELIISLOG.WSF):
Copy Code code as follows:

<job id= "IIS log Cleanup WSH version (DELIISLOG.WSF)" >
<script language= "VBScript" >
' Author: Liu YONGFA (yongfa365) ' Blog
' Modified: 2007-11-15
' Operation Description: This file can only clear a day's log, you have to use the scheduled task to run it once a day, because the server is generally disabled Wscript.Shell, so do not recommend the use of
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 this if you want to test, you will not directly on the server test it, so, the following posted my Liu Yong test script, mainly in D disk to generate a test folder, and some IIS test log files,
IIS log Cleanup-generation system (Createiislog.vbs):
Copy Code code as follows:

IIS log cleanup for IIS logs generation system (Createiislog.vbs)
' Create a 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 in the IIS log processing, but also in the processing of serv-u log processing, provided that the Serv-u log file format can also be set to imaging IIS log format Ex071115.log.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.