Login   /   Register

Delete all files older than a specific date

Rate this article
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...
March 31st, 2008 by daniva
Option Explicit
 
Public Function DeleteOldFiles( ByVal folderPath, ByVal daysOld )
    Dim fso, folder, files, file
    Dim dateCreatedStr, daysDiff
 
    DeleteOldFiles = -1
    Set fso = CreateObject( "Scripting.FileSystemObject" )
    If Not fso.FolderExists( folderPath ) Then
         Reporter.ReportEvent micWarning, "DeleteOldFiles", "Folder -> " & folderPath & " was not found."
         Exit Function
    End If
    Set folder = fso.GetFolder( folderPath )
    ' ** Retrieve all the files in the folder
    Set files = folder.Files
    DeleteOldFiles = 0
    For Each file In files
        dateCreatedStr = file.DateCreated
        daysDiff = DateDiff( "d", dateCreatedStr, Date )
        If daysDiff > daysOld Then
            fsoutil.DeleteFile file.Path, True
            DeleteOldFiles = DeleteOldFiles + 1
        End If
    Next
End Function

We have function that removes older files than x days in a specific folder.

The two arguments of the function give us the flexibility to choose the folder and the day’s factor argument. The function will return -1, in case of an error, or a number, in case of success.

Note that 0 (zero) is also considered a success, zero means that the folder was empty.

The return default value of the function is always failed ( -1 ).

DeleteOldFiles = -1

After setting the initial default return value of the function, we make a folder validation, to avoid popup errors, and to warn the test that the folder not found, also a detailed message to the reporter is been written, to understand the error on long scripts.

If Not fso.FolderExists( folderPath ) Then
     Reporter.ReportEvent micWarning, "DeleteOldFiles", "Folder -> " & folderPath & " was not found."
     Exit Function
End If

After we sure, that the folder exists, then we retrieve all the files on the specific folder and then the function loops the file collection.a file age is usually defined by the File Creation Date ( DateCreated property )

The condition is the DateDiff VBScript function.

Since the argument of the function measured in days, we use the interval "d". Consider this, if you want to define your own function with custom intervals.

dateCreatedStr = file.DateCreated
daysDiff = DateDiff( "d", dateCreatedStr, Date )
If daysDiff > daysOld Then
    fsoutil.DeleteFile file.Path, True
    DeleteOldFiles = DeleteOldFiles + 1
End If

When the condition is True, the file deletion counter increasing by one, to report, how many files removed.This is how to call the function, if you decide to write the function in an external associated function library.

deletedFilesCount = DeleteOldFiles( "C:\LOG", 1 )
MsgBox deletedFilesCount & " files deleted from C:\LOG"

Posted in FileSystemObject

Leave a Reply

You must be logged in to post a comment.

This article was viewed 486 times