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

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, 2011 Hoan 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
  • Dusit

    ็Hi, What i can use function if i would like to delete all file instead Logs file(Call DeleteOldLogFiles), please kindly advise me

    • http://4rapiddev.com/ Hoan Huynh

      Hi Dusit,

      Simply specify the folder you would like to delete files, let’s say: D:/tmp for example.

      Hope it’s clear.
      H2.

  • Dusit

    Just specify directory will delete all files in folder and subfolder right?

    • http://4rapiddev.com/ Hoan Huynh

      Just delete all files in this folder in the example. Not sub folders.

  • Anonymous

    Thank  You, this worked perfectly on my IIS7 server!

Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com

7 years ago Tips And TricksCreateObject, FileSystemObject, GetFolder, VB Script, WScript961
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
16,466 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
12,284 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
11,623 views
JQuery Allow only numeric characters or only alphabet characters in textbox
9,611 views
PHP Call Web Service WSDL Example
6,451 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
  • Magento Fatal error: Unsupported operand types
  • Ionic bower the name contains uppercase letters
  • PHP Magento Get Manufacturer Name And ID From Magento Product ID
  • Know Simple Hacks To Systematize Media Library in WordPress
  • A handy guideline on adding custom menu item in WordPress admin
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (26)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (140)
EMAIL SUBSCRIPTION

Sign up for our newsletter to receive the latest news and event postings.

Recommended
  • CDN
  • Hosting
  • Premium Themes
  • VPS
2014 © 4 Rapid Development