IIS log cleanup cmd, vbs, JS, and wsh

Source: Internet
Author: User
'Iis log cleanup-IIS log 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
Next

'*************************************** *******************
<Job id = "IIS log cleanup wsh Code (Deliislog. WSF)
<Script language = "VBScript">
'Author: Liu yongfa (yongfa365) 'blog
'Modified:
'Operation instructions: This file can only clear logs for one day. You have to use a scheduled task to execute it once a day. Because wscript. Shell is generally disabled on the server, it is not recommended to use
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 log cleanup vbs code (deliislog. vbs)
'Call method: deliislog "IIS Log Path", how many days of IIS logs are retained

'Traverse all files in the IIS log folder and files in the subfolders
Function deliislog (iislogpath, keepdays)
On Error resume next
Set ofso = Createobject ("scripting. FileSystemObject ")
Set ofolder = ofso. getfolder (iislogpath)
Set osubfolders = ofolder. subfolders to get the set of all folders in the directory.
Set ofiles = ofolder. Files '.
'Step 1: process all files in the current directory
For each ofile in ofiles 'traverse all 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' determines whether the IIS log file is to be processed. If yes, delete it directly.
End if
Next
'Step 2: process all the directories in the current directory and perform recursive calls
For each osubfolder in osubfolders
Deliislog osubfolder. Path, keepdays 'recursion
Next

End Function

Deliislog "D: \ iislogtest", 20' Traversal

'*************************************** *******************
// IIS log cleanup JS Code (deliislog. JS)
// Call method: deliislog ("IIS Log Path", number of days of IIS log retained );

// Traverse all files in the IIS log folder and files in the subfolders
Function deliislog (iislogpath, keepdays ){
VaR FSO = new activexobject ("scripting. FileSystemObject ");
VaR F = FSO. getfolder (iislogpath );
VaR folders = new enumerator (F. subfolders); // get the set of all folders in the directory
VaR files = new enumerator (F. Files); // obtain the set of all files in the directory
// Process all 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 (); // you can check whether the IIS log file is to be processed. If yes, delete it directly.
}
// Step 2 process all the directories in the current directory and perform recursive calls
For (;! Folders. atend (); folders. movenext ()){
Deliislog (folders. Item (), keepdays );
}
}
// Call a function, for example, "F: \ log", 5, or "C: \ Windows \ system32 \ logfiles", 5
Deliislog ("D: \ iislogtest", 2 );

'*************************************** *******************
'Iis log cleanup cmd code (deliislog. cmd)
@ Echo off
Title clean log
: Set the number of days before or after the current date
Set/A beforedays =-3
: Set the directory location
Set dir = "F: \ log \"
: Converts the current date to the number of days for calculation.
Call: date2day % Date :~ 0, 10% days
Set/A days = % days % beforedays %
Call: day2date % days % lastdate
: After calculation, 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

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.