SoFunction
Updated on 2025-04-09

Recommended code to clean iis logs with multiple scripts page 2/3


Example code:

IIS log cleaning CMD version code():

Copy the codeThe code is as follows:

@echo off 
:: Set how many days before or after the current date
set/a beforedays=-3 
::Set the directory location
set   dir="F:\log\" 
::Convert the current date to days and calculate it
call :Date2Day %date:~0,10% days  
set/a days=%days%%beforedays% 
call :Day2Date %days% lastdate 
::The calculation is completed and the desired character combination is generated.
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 cleaning VBS version code():
Copy the codeThe code is as follows:

'IIS log cleaning VBS version code()  Made by
'Call method: DelIISLog "Path where the IIS log is located", how many days of IIS log are retained

'Travel through all files in the IIS log folder and files in the subfolder
Function DelIISLog(IISLogPath,KeepDays)    
on error resume next   
    Set oFso = CreateObject("")    
    Set oFolder = (IISLogPath)    
Set oSubFolders =   'Get a collection of all folders in this directory
Set oFiles =  'Get a collection of all files in this directory
'The first step is to process all files in the current directory
For Each oFile In oFiles 'Transfer all files
        if right(,3)="log" then   
            oDate=cdate("20" & mid(,3,2) & "-" & mid(,5,2) & "-" & mid(,7,2))    
if date-oDate > KeepDays then 'Just determine whether it is the IIS log file to be processed, and delete it directly if it is.
        end if   
    Next   
'The second step is to process all directories in the current directory and make recursive calls
    For Each oSubFolder In oSubFolders    
DelIISLog,KeepDays'Recursion
    Next   

End Function   

DelIISLog "D:\IISLogTest",20 'Travel

'IIS log cleaning VBS version code()  Made by
'Call method: DelIISLog "Path where the IIS log is located", how many days of IIS log are retained

'Travel through all files in the IIS log folder and files in the subfolder
Function DelIISLog(IISLogPath,KeepDays) 
on error resume next 
    Set oFso = CreateObject("") 
    Set oFolder = (IISLogPath) 
Set oSubFolders =   'Get a collection of all folders in this directory
Set oFiles =  'Get a collection of all files in this directory
'The first step is to process all files in the current directory
For Each oFile In oFiles 'Transfer all files
        if right(,3)="log" then 
            oDate=cdate("20" & mid(,3,2) & "-" & mid(,5,2) & "-" & mid(,7,2)) 
if date-oDate > KeepDays then 'Just determine whether it is the IIS log file to be processed, and delete it directly if it is.
        end if 
    Next 
'The second step is to process all directories in the current directory and make recursive calls.
    For Each oSubFolder In oSubFolders 
DelIISLog,KeepDays'Recursive
    Next 

End Function 

DelIISLog "D:\IISLogTest",20 

'Traveling IIS logs to clean up JS version code():
Copy the codeThe code is as follows:

//IIS log cleaning JS version code() Made by
//Call method: DelIISLog("Path where the IIS log is located", how many days of IIS log are retained);

//Travel through all files in the IIS log folder and files in the subfolder
function DelIISLog(IISLogPath,KeepDays){    
    var fso = new ActiveXObject("");    
    var f = (IISLogPath);    
var Folders = new Enumerator();//Get a collection of all folders in this directory
var Files = new Enumerator();//Get a collection of all files in this directory
//The first step is to process all files in the current directory
    for (; !(); ()) {    
        var fileName = ().name;    
        var year = "20" + (2, 2);    
        var mouth = (4, 2);    
        var day = (6, 2);    
        var days = (((new Date()).getTime() - (year, mouth - 1, day)) / 1000 / 60 / 60 / 24);    
if (days> KeepDays) ().Delete(); //Defend whether it is the IIS log file to be processed, and delete it directly if it is.
    }    
//The second step is to process all directories in the current directory and make recursive calls.
    for (; !(); ()) {    
        DelIISLog((),KeepDays);    
    }    
}    
//Calling functions, such as: "F:\\log",5  or "C:\\windows\\system32\\LogFiles",5
DelIISLog("D:\\IISLogTest",2);   

//IIS log cleaning JS version code()  Made by
//Call method: DelIISLog("Path where the IIS log is located", how many days of IIS log are retained);

//Travel through all files in the IIS log folder and files in the subfolder
function DelIISLog(IISLogPath,KeepDays){ 
    var fso = new ActiveXObject(""); 
    var f = (IISLogPath); 
var Folders = new Enumerator();//Get a collection of all folders in this directory
var Files = new Enumerator();//Get a collection of all files in this directory
//The first step is to process all files in the current directory
    for (; !(); ()) { 
        var fileName = ().name; 
        var year = "20" + (2, 2); 
        var mouth = (4, 2); 
        var day = (6, 2); 
        var days = (((new Date()).getTime() - (year, mouth - 1, day)) / 1000 / 60 / 60 / 24); 
if (days > KeepDays) ().Delete(); //Defend whether it is the IIS log file to be processed, and delete it directly if it is.
    } 
//The second step is to process all directories in the current directory and make recursive calls.
    for (; !(); ()) { 
        DelIISLog((),KeepDays); 
    } 

//Calling functions, such as: "F:\\log",5  or "C:\\windows\\system32\\LogFiles",5
DelIISLog("D:\\IISLogTest",2);

IIS log cleaning WSH version code():
Copy the codeThe code is as follows:

<job >    
    <script language="vbscript">    
'Author: Liu Yongfa (yongfa365)'Blog
'Modification: 2007-11-15
'Operation Instructions: This file can only clear logs for one day. You must use the scheduled task to execute it once a day. Because it is generally disabled on the server, it is not recommended to use it.
        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 = ("")    
             p    
             (" /c del /s " & p)    
            Set WshShell = Nothing   
        End Function   

        DelIISLog "D:\IISLogTest",2    
    </script>    
</job>   

<job > 
    <script language="vbscript"> 
'Author: Liu Yongfa (yongfa365)'Blog
'Modification: 2007-11-15
'Operation Instructions: This file can only clear logs for one day. You must use the scheduled task to execute it once a day. Because it is generally disabled on the server, it is not recommended to use it.
        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 = ("") 
             p 
             (" /c del /s " & p) 
            Set WshShell = Nothing 
        End Function 

        DelIISLog "D:\IISLogTest",2 
    </script> 
</job>

Previous page123Next pageRead the full text