Cleanup Old Log Files (revisited)

Back in Nov-2006 I wrote about a free script (WaRmZip) you could download to be used for deleting old log files. That script includes a lot of features and flexibility.

Here’s a simpler script that does a similar cleanup of old log files that you might find useful. It includes a .vbs backend script plus a simple one line .bat batch file script that can be added to your SBS scheduler.

1. You can download a zip file from my website that contains the .vbs file, a sample .bat file and a sample log file. To keep things simply, I store these files on my SBS server’s in a C:\scripts folder.

2. The .vbs file does not need any editing at all. It is designed to search, starting at a specified folder, plus all sub folders below it. Howerver, if you wish to play it safe while testing, you can edit the .vbs file and remark out the file.delete line located on line 37 near the end of the script.

3. The .bat file will define the starting folder and the age of files to be deleted, and will create a log file so you can track the files deleted. This batch file can then be scheduled to run daily, weekly or whatever. You can either create separate batch files or just add additional cscript lines to this one batch file.

Here’s an example of the batch file IU use to cleanup IIS log files over 30 days old:

cscript c:\scripts\deloldfiles.vbs c:\windows\system32\logfiles 30 >> c:\scripts\iis.log

And here is part of the corresponding log file it creates:

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Today:12/29/2007 8:32:06 AM
Started deleting files older than :11/29/2007 8:32:06 AM
Deleting Files under the Folder:C:\WINDOWS\system32\LogFiles
Deleting C:\WINDOWS\system32\LogFiles\IN071105.log last modified: 11/5/2007 7:18:34 PM
Deleting C:\WINDOWS\system32\LogFiles\IN071110.log last modified: 11/10/2007 3:47:43 PM
Deleting C:\WINDOWS\system32\LogFiles\IN071118.log last modified: 11/18/2007 11:55:00 AM
Deleting C:\WINDOWS\system32\LogFiles\IN071123.log last modified: 11/23/2007 11:43:26 PM
Completed deleting files older than :11/29/2007 8:32:06 AM


18 thoughts on “Cleanup Old Log Files (revisited)

  1. Hi, this works nicely for what I was looking for.

    I used it on an internal ftp-server on which files are only allowed to stay for 7 days without removing the folder structure.


  2. Works great but how do I have it delete the folders as well? Also is it possible to only delete/search folders that start with says backup*

  3. Hi,

    I don´t know what happend when I using \\server_name\share_folder, write in the log but don´t remove the files. When I use the Leter is working.

  4. This is a great script, But I also want to select and delete specific files like “.log and .duplicate” files only.

    How can I modify the script to do so?

    please help

  5. Very clean and simple solution.

    My (small) modification was to delete all files older than 30 days that weren’t created on the 1st of a month, which I find useful for ETL logs where I may want to check the timings of a build from 18 months ago.

    if (file.datelastmodified < newdate then became if (file.datelastmodified < newdate AND day(file.datelastmodified) <> 1) then

    Thanks again,

Leave a Reply

Your email address will not be published. Required fields are marked *