• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

macro getting runtime 445 error when running

RAM72

Member
Got a code but trying to delete all csv in folders and subfolders but getting error message

Set fs = Application.FileSearch



Object doesn't support this action runtime 445 as above

Can anyone assist what is going wrong

Code:
Sub delkillcsv()
Set fs = Application.FileSearch
With fs
    .LookIn = "C:\Users\obbdg\Desktop"
    .SearchSubFolders = True
    .Filename = "*.csv"
    If .Execute() > 0 Then
            For i = 1 To .FoundFiles.Count
            Kill .FoundFiles(i)
        Next i
        MsgBox .Filename & " " & .FoundFiles.Count & " No. files found."
    Else
        MsgBox "There were no files found."
    End If
End With
End Sub
 
Here's sample code to recursively go down folder structure and to delete csv files.

Code:
Dim FSO As Object
Sub delkillcsv()
Dim myFiles As Object, file As Object
Dim startFold As String

Set FSO = CreateObject("Scripting.FileSystemObject")
startFold = "C:\Test\Demo"

If Right(startFold, 1) = "\" Then
    startFold = Left(startFold, Len(startFold) - 1)
End If

Set myFiles = FSO.GetFolder(startFold).Files

For Each file In myFiles
    If InStr(file.Name, ".csv") Then
        Kill file.Path
    End If
Next

Call chkSubfolder(FSO.GetFolder(startFold))
End Sub

Sub chkSubfolder(fold As Object)
Dim subfolder As Object, fileCol As Object, file As Object

For Each subfolder In fold.Subfolders
    Set fileCol = FSO.GetFolder(subfolder.Path).Files
    For Each file In fileCol
        If InStr(file.Name, ".csv") Then
            Kill file.Path
        End If
    Next
    Call chkSubfolder(subfolder)
Next

End Sub

Edit: Actually it may be safer to do... Right(file.Name, 4) = ".csv" rather than use Instr. Just in case someone has odd file name like "mydoc.csv.xlsx"
 
Last edited:
Tested works as charm, thank you a lot just an insight from me my apology , how to delete both csv, xls format only at same time.

where the need to adjust the code
 
You see where Instr is used to check for file extension in code.

Just add OR condition and check for .xls as well. As mentioned in post, it may be safer to use Right function instead of Instr.
 
Back
Top