DelOldFiles

Delete files in a specified folder which are older than x days
This code snippet does just that!
You can call the code with a timer so you have an automated directory scanner.

Public Function DelOldFiles(delDays as integer, scanDir as string)
'-----------------------------------------------------'
' Procedure :  delOldFiles
' Description:  DELETE OLD FILES
' Date-Time :  5/16/2002-4:26:37 PM
' Parameters :  delDays -> number of days, scanDir -> the directory
'-----------------------------------------------------'
 Dim fs As Object
 On Error GoTo errorHandler
 Set fs = CreateObject("Scripting.FileSystemObject")
 ' CHECK IF THE DIRECTORY EXISTS
 If Dir$(scanDir, vbDirectory) <> "" Then
  ' DIRECTORY DOES EXIST, SO GO ON ...
  Dim fileDate As Date ' Declare variables.
  Dim f, f1, fc
  Set f = fs.GetFolder(scanDir)
  Set fc = f.Files
  For Each f1 In fc
   fileDate = f1.DateCreated
   If DateDiff("d", fileDate, Now) > delDays Then
    f1.Delete
   End If
  Next
 End If
 Set fs = Nothing
 Exit Function
ErrorHandler:
 Resume Next
End Function

delDays as integer, scanDir as string

Views 2760 Downloads 860


ANmarAmdeen
612
File System VBA
Revisions

v1.0