Function DeleteDailyCsvFiles(ByVal theFolder As String, ByVal prefix As String, ByVal ignoreErrors As Boolean) As Long
' Note: does not check sub-folders !
' Calling sequence:
' dim numFilesDeleted as long
' numFilesDeleted = DeleteDailyCsvFiles("C:\myfolder", "XYZ_DAILY", True)
Dim oneFile As Variant
Dim fldr As String
Dim lcasePrefix As String
If InStrRev(theFolder, "\") = Len(theFolder) Then
fldr = theFolder
Else
fldr = theFolder & "\"
End If
DeleteDailyCsvFiles = 0
lcasePrefix = LCase(prefix)
oneFile = Dir(fldr & "*.csv")
If ignoreErrors Then On Error Resume Next
While (oneFile <> "")
If InStr(LCase(oneFile), lcasePrefix) = 1 Then
Kill fldr & oneFile
DeleteDailyCsvFiles = DeleteDailyCsvFiles + 1
End If
oneFile = Dir
Wend
If ignoreErrors Then On Error GoTo 0 ' Reset[/INDENT]
End Function