help to modify a code to delete 2 specific file extensions instead of 1

learn99

New Member
Joined
Jul 11, 2017
Messages
10
I got a code while goggling, the code looks for csv and delete in subfolders .

the actual code delete csv only

note just learning.

Assistance what to modify in the code to delete to delete csv and xlsb only:confused:


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
[B]    If InStr(file.Name, ".csv") Then[/B]
        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
[B]        If InStr(file.Name, ".csv") Then[/B]
            Kill file.Path
        End If
    Next
    Call chkSubfolder(subfolder)
Next


End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
have you tried doing the loop again

Code:
For Each file In myFiles
[B]    If InStr(file.Name, ".csv") Then[/B]
        Kill file.Path
    End If
Next
For Each file In myFiles
[B]    If InStr(file.Name, ".xlsb") Then[/B]
        Kill file.Path
    End If
Next
 
Upvote 0
Tried but got message

compile error
For control variable already in use


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
[B]    If InStr(file.Name, ".csv") Then[/B]
        Kill file.Path
    End If
Next


For Each file In myFiles
[B]    If InStr(file.Name, ".xls") Then[/B]
        Kill file.Path
    End If
Next
Call chkSubfolder(FSO.GetFolder(startFold))End SubSub chkSubfolder(fold As Object)Dim subfolder As Object, fileCol As Object, file As ObjectFor Each subfolder In fold.Subfolders    Set fileCol = FSO.GetFolder(subfolder.Path).Files    \z    Call chkSubfolder(subfolder)NextEnd Sub</pre>
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,426
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top