Thursday, May 11, 2017

Delete IISLogs

'== This is the VB script which will delete the IIS logs which are older than 60 days from each site folder and write log about number of files deleted==='

intDeletedCount = 0

'===Append log file ========= '
Const ForAppending = 8

'===Create Log File ========= '
Set ObjForLog = CreateObject("Scripting.FileSystemObject")
Set ObjLogFile = ObjForLog.OpenTextFile("G:\Scripts\IISLogsCleanUp\log.txt", ForAppending, True)


' ======== Delete IIS log files ======== '
sLogFolder = "C:\inetpub\logs\LogFiles"
DelDate = 60   'in days
Set objFSO = CreateObject("Scripting.FileSystemObject")
set colFolder = objFSO.GetFolder(sLogFolder)
For Each colSubfolder in colFolder.SubFolders
        Set objFolder = objFSO.GetFolder(colSubfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
                iFileAge = now-objFile.DateLastModified
                if iFileAge > (DelDate+1)  then
                        objFSO.deletefile objFile, True
intDeletedCount = intDeletedCount + 1
                end if
        Next
Next


' ====== Count number of deleted file ====== '
intDeletedCount = intDeletedCount + 1

'======= Write Log file ====================='
ObjLogFile.WriteLine (Now & " -- " & intDeletedCount-1 & " IISLog(s) deleted older than 60days.")
ObjLogFile.Close


Set ObjForLog = Nothing
Set ObjLogFile = Nothing
Set objFSO = Nothing
Set colFolder = Nothing
Set objFolder = Nothing
Set colFiles = Nothing