Avatar of Rayne
Rayne
Flag for United States of America asked on

Delete this

Hello

I am trying to look for this thing but hard time finding this - using FSO -
1. open a main folder and then
2. go through each sub folder under that main folder and if you see any files, simply delete it

How can i do that in VBA?

Thank You
Rayne
Microsoft OfficeMicrosoft Excel

Avatar of undefined
Last Comment
Rayne

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
byundt

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rayne

ASKER
thanks Bryan :)
Rayne

ASKER
does what i need but in a very elegant way of coding - recursion - the very basics of CS classes :)
byundt

I revised the code so it would delete the subfolders as well as the files. I also added the ability to specify a file naming pattern, i.e. delete only files like *.xl*, *.pdf or *.*
Dim FSO As Object

Sub RecursiveFileDeletion()
'Deletes files of a specified name pattern within a user-specified folder and its subfolders. Uses recursion.
Dim sNamePattern As String, TopFolderName As String
Dim TopFolderObj As Object
sNamePattern = "*.*"        'Delete just Excel files *.xl*, delete all files *.*

TopFolderName = Application.GetOpenFilename("Files (" & sNamePattern & "), " & sNamePattern, _
        Title:="Pick any file in desired folder, then click Open, EE Q28312844")
If TopFolderName = "False" Then Exit Sub
TopFolderName = Left(TopFolderName, InStrRev(TopFolderName, Application.PathSeparator) - 1)

Application.ScreenUpdating = False
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TopFolderObj = FSO.GetFolder(TopFolderName)

SubFolderRecursion TopFolderObj, sNamePattern
Set FSO = Nothing
MsgBox "Done"
End Sub

Sub SubFolderRecursion(OfFolder As Object, sNamePattern As String)
Dim SubFolder As Object
SearchFolder OfFolder.Path, sNamePattern
For Each SubFolder In OfFolder.SubFolders
    SubFolderRecursion SubFolder, sNamePattern
    SearchFolder SubFolder.Path, sNamePattern
    On Error Resume Next
    If Dir(SubFolder.Path & "\*.*") = "" Then RmDir SubFolder.Path      'Delete the subfolder if it is empty
    On Error GoTo 0
Next SubFolder
End Sub
    
Sub SearchFolder(strPath As String, sNamePattern As String)
Dim strFile As String
strFile = Dir(strPath & "\" & sNamePattern)
On Error Resume Next
Do While strFile <> ""
    If strFile <> ActiveWorkbook.Name Then Kill strPath & "\" & strFile
    strFile = Dir
Loop
On Error GoTo 0
End Sub

Open in new window


Brad
RecursiveFileDeleterQ28312844.xlsm
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Rayne

ASKER
Apologize for not getting your name right in the first place :(
I sometimes mistake one expert from other based of their usernames.

Thank you Brad - that is awesome - 5 stars for the file type thing :) as I knew there would be multiple file types situation possible in my case.