Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home Tips And Tricks Auto Delete Old IIS Logs, FTP Logs, SMTP Logs In Windows

Auto Delete Old IIS Logs, FTP Logs, SMTP Logs In Windows

I’ll provide a VB Script that I’m running on a Windows 2008 dedicated server; it will delete all old log files (IIS, FTP and SMTP) automatically after a specified number of days.

Keep all log files is great idea to analytic the usage of a particular service and especially when the service is under attack, you need to know exactly Who attack you/Where attacker comes from (based on their IP address), How often they send the request to your server (based on the timestamp) and Which file/location they send the request to, etc to investigate your issue.

However, if these logs are kept for a long time, your hard drives may be full up and your services may stop working. Therefore, create a schedule to clean up all old log files after a specified number of days automatically is the second idea you need to think about.

VB Script delete log files older than a defined day

This script defines DeleteOldLogFiles() function which will scan a specified log folder and delete all files older than a specified number of days.

Option Explicit 
 
' Delete all IIS Log Files older than 30 days
Call DeleteOldLogFiles("C:/WINDOWS/system32/LogFiles/W3SVC2", 30)
' Delete all FTP Log Files older than 30 days
Call DeleteOldLogFiles("C:/WINDOWS/system32/LogFiles/MSFTPSVC1", 30)
' Delete all SMTP Log Files older than 30 days
Call DeleteOldLogFiles("C:/WINDOWS/system32/LogFiles/SMTPSVC1", 30)
 
Function DeleteOldLogFiles(strPath2Logs, intDays2Keep) 
    Dim fso, f, fc, f1, strFiles
 
	strFiles = ""
 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    If (fso.FolderExists(strPath2Logs)) Then 
        Set f = fso.GetFolder(strPath2Logs) 
        Set fc = f.Files 
 
        '-- Determine if file is older than defined days 
        For Each f1 in fc 
            If DateDiff("d", f1.DateLastModified, Now) > intDays2Keep Then 
				strFiles = strFiles & f1.Name & vbCrLf 
				WScript.Echo "Deleted file: " & f1.Name & " - " & DateDiff("d", f1.DateLastModified, Now)
				f1.Delete(True)
            End If 
        Next 
 
        Set f1 = Nothing 
        Set fc = Nothing 
        Set f = Nothing 
    End If 
    Set fso = Nothing 
 
End Function

Option Explicit ' Delete all IIS Log Files older than 30 days Call DeleteOldLogFiles("C:/WINDOWS/system32/LogFiles/W3SVC2", 30) ' Delete all FTP Log Files older than 30 days Call DeleteOldLogFiles("C:/WINDOWS/system32/LogFiles/MSFTPSVC1", 30) ' Delete all SMTP Log Files older than 30 days Call DeleteOldLogFiles("C:/WINDOWS/system32/LogFiles/SMTPSVC1", 30) Function DeleteOldLogFiles(strPath2Logs, intDays2Keep) Dim fso, f, fc, f1, strFiles strFiles = "" Set fso = CreateObject("Scripting.FileSystemObject") If (fso.FolderExists(strPath2Logs)) Then Set f = fso.GetFolder(strPath2Logs) Set fc = f.Files '-- Determine if file is older than defined days For Each f1 in fc If DateDiff("d", f1.DateLastModified, Now) > intDays2Keep Then strFiles = strFiles & f1.Name & vbCrLf WScript.Echo "Deleted file: " & f1.Name & " - " & DateDiff("d", f1.DateLastModified, Now) f1.Delete(True) End If Next Set f1 = Nothing Set fc = Nothing Set f = Nothing End If Set fso = Nothing End Function

To make it cleans up the old logs automatically, we may need to create a Task Scheduler which will run that script every day as below:

Task Scheduler Create Schedule

Task Scheduler Create Schedule

Task Scheduler Create Action

Task Scheduler Create Action

+ Download the Delete Old Log Files source above

Sep 23, 2011Hoan Huynh
How To Ask People To Like Your Facebook Page On The Landing PagePHP CURL Post To HTTPS Website
You Might Also Like:
  • PHP Delete File Function
  • Delete WordPress Revision Posts, Its Relationships And Meta Data
  • Delete Bulk Draft And Trash WordPress Posts Includes All Tags, Comments, Meta Fields And Terms Associated
  • WordPress Delete Unused Post Tags By SQL Command
  • PHP Completely Delete A Directory Include Sub Directories And Files
  • How To Delete Application In Facebook Developers
  • Auto Rotate Web Page Title With JavaScript
  • Delete, Shrink, Eliminate Transaction Log .LDF File
  • HTTP/ HTTPS Document, CGI-BIN And Access/ Error Logs On cPanel, Plesk And VirtualMin
  • Remove/Delete your saved passwords in IE, Firefox, Google Chrome
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago Tips And TricksCreateObject, FileSystemObject, GetFolder, VB Script, WScript1,360
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,164 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,040 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,821 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,296 views
C# Read Json From URL And Parse/Deserialize Json
9,790 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Installment Loans – Making Sense of Online Software
  • Apple Pay Casino Canada
  • Casinos austria
  • Essay For Sale – How To Write A Superb One That Will Sell
  • Free Online Photo Editor
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (103)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development