VB Script to clear out log files

Posted on 2007-10-18
Last Modified: 2012-08-13
I need a script that will automatically clear out  log files older then a week and runs daily.   I'm a complete novice when it comes to VBScript so any and all help setting this up would be appreciated.  
Question by:bpp2444
    LVL 12

    Expert Comment

    Are you referrring to the log files in the eventviewer or the log files stored in any of the folders.

    Author Comment

    Both would actually be helpful ;)
    LVL 12

    Accepted Solution

    Try this to delete the log files in a specific folder. Change the location to suit your needs...

    Dim oFSO
    Dim oFolder
    Dim oFile
    Dim iDaysOld

    'Customize values here to fit your needs
          Set oFSO=CreateObject("Scripting.FileSystemObject")
          set oFolder=oFSO.GetFolder("C:\Scripts\logss")

    'Walk through each file in this folder collection.
    'If it is older than 7 days, then delete it.

    For each oFile in oFolder.Files
    If oFile.DateLastModified < (Date() - iDaysOld) Then                               
    oFSO.DeleteFile oFolder.Path & "\" & oFile.Name
    End If

    'Clean up
          Set oFSO = Nothing
          Set oFolder = Nothing
          Set oFileCollection = Nothing
          Set oFile = Nothing

    This will delete all the files in the folder C:\Scripts\logss. If you want we can change this to delete specific file extensions

    Author Comment

    Alright I have to add to this cause like I said I'm really new to this, in turn I will bump the points up to 500 ;)))  What is my best option to set this up to run daily at a specific time???  Do I just put this in a specific folder and set a basic dos script to run everyday?  Thank you for your help chandru sol.  
    LVL 12

    Expert Comment


    I would suggest just run a scheduled task daily and you can select the file. Before that you save the above script as delold.vbs and select the same file in the scheduled task to run everyday.


    Author Comment

    Thats what I was thinking just wanted to be sure, good deal ;)))  I will try this out.  
    LVL 65

    Expert Comment

    Hi guys, just thought I'd put in my 2 cents....Event Logs from the Event Viewer cannot be partially cleared.  It is all or nothing......but they *can* be backed up and stored for import again later.



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now