delete all excel files in subfolders

BradMS

New Member
Joined
Jan 27, 2017
Messages
32
Im missing something here.
I would like to delete all *.xlsx files in this folder including all subfolders that might be in this folder.


VBA Code:
Sub Clear_All_Files_And_SubFolders_In_Folder2()
'Delete all files and subfolders
'Be sure that no file is open in the folder
    Dim fso As Object
    Dim MyPath As String
    Set fso = CreateObject("scripting.filesystemobject")
    MyPath = "c:\tc"  '<< Change
    If Right(MyPath, 1) = "\" Then
        MyPath = Left(MyPath, Len(MyPath) - 1)
    End If
    If fso.FolderExists(MyPath) = False Then
        MsgBox MyPath & " doesn't exist"
        Exit Sub
    End If
    On Error Resume Next
    'Delete files
    fso.DeleteFile MyPath & "\*.XLSX", True
    'Delete subfolders
    fso.deletefolder MyPath & "\*.XLSX", True
    On Error GoTo 0
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
This is it.


Public Sub delWB()
Dim FSO As Object
Dim folder As Object, subfolder As Object
Dim wb As Object

Set FSO = CreateObject("Scripting.FileSystemObject")
folderPath = "Z:\crc"
Set folder = FSO.GetFolder(folderPath)

For Each wb In folder.Files
If LCase(Right(wb.name, 3)) = "xls" Or LCase(Right(wb.name, 4)) = "xlsx" Or LCase(Right(wb.name, 4)) = "xls" Then
FSO.DeleteFile wb, True
End If
Next
For Each subfolder In folder.SubFolders
For Each wb In subfolder.Files
If LCase(Right(wb.name, 3)) = "xls" Or LCase(Right(wb.name, 4)) = "xlsx" Or LCase(Right(wb.name, 4)) = "xls" Then
FSO.DeleteFile wb, True
End If
Next
Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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