Delete Files From Subfolders

Delta Star

Board Regular
Joined
Oct 28, 2009
Messages
184
I have a folder which contains a large number of subfolders, each of which contain various excel files.

Is it possible for a macro to run through each of the subfolders and delete the files automatically without deleting the actual folders?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Sure - how 'deep' is the subfolder structure? ie is it only one level down or will there be multiples? Will you want to delete all files in each subfolder level or just the bottom one?

The following will delete all xls files from the subfolders in SomeOtherFolder:

Code:
Sub Delete_Files()
Dim fso, fldr, subfldr, fl

Set fso = CreateObject("scripting.filesystemobject")

Set fldr = fso.GetFolder("C:\SomeFolder\SomeOtherFolder")

On Error Resume Next

For Each subfldr In fldr.SubFolders
    For Each fl In subfldr.Files
        If Right$(fl.Name, 3) = "xls" Then
            Kill fl.Path
        End If
    Next fl
Next subfldr

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,280
Members
452,902
Latest member
Knuddeluff

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