Gerard's codebase

This is a personal repository of code snippets and information.

Over the years I have generated lots of little sub programs and stored away acres of useful code snippets. The problem is always to locate them.

Even more time wasting is forgetting how to do simple things when you havnt developed in an environment for a few years (or even a few months)

My new years resolution is to start putting them up in a common place as I produce them. (thanks google)

They are handy for me and, with a bit of a clean up and documentation, they might be handy for others if they wander in here.

Gerard 2008

Thursday, June 5, 2008

Deleting Log Files

Script to clean up old files.

This is a handy little disk maintenance script I put together to delete files older then x days.

It was written to delete log and text files that custom data feeds produce and which previously had to be deleted manually.

It needs to be saved with a .vbs extension (visual basic scripting). Just add a call to DeleteOldFiles for each directory and set it up under Scheduled tasks.

If you use it, find it useful or have any suggestions/bugs then please leave a comment to let me know. Thanks Gerard


'Gerard Conlon May 2008
'This VB script deletes files older then a certain date in a specifed directory
'The following call will delete all .log and .txt files in the c:\test dir older then 30 days

DeleteOldFiles "c:\test",30, ".log .txt"


'#####################################################
Sub DeleteOldFiles(sDir, iDaysOldToDelete, sExts)
'sDir is the drive and path to the dir for the files to be deleted
'iDaysOldToDelete is the number of days back to delete
'sExts is file extensions to delete - only 3 char extensions preceeded by the dot
Dim oFSO, oFolder, oFiles, oFile
Dim arrSplitName, strExtension
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDir)
Set oFiles = oFolder.Files
For Each oFile In oFiles
'check if it is one of the extensions passed in
If InStr(sExts, LCase(Right(oFile.Name, 4))) Then
'if the last modified date + the age period is less then now then delete the file
If DateAdd("d", iDaysOldToDelete, oFile.DateLastModified) < ofiles =" Nothing" ofolder =" Nothing" ofso =" Nothing">