Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete this

Posted on 2013-12-06
5
Medium Priority
?
239 Views
Last Modified: 2013-12-07
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
0
Comment
Question by:Rayne
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 2000 total points
ID: 39702735
The following code will let the user pick any file in a folder. It will then delete any file within that folder plus any subfolders. The empty subfolders will not be deleted.
Dim FSO As Object

Sub SnarkHunt()
Dim TopFolderName As String
Dim TopFolderObj As Object
        'Input Path and Search Term
TopFolderName = Application.GetOpenFilename("Files (*.*), *.*", _
        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
Set FSO = Nothing
MsgBox "Done"
End Sub

Sub SubFolderRecursion(OfFolder As Object)

Dim SubFolder As Object
SearchFolder OfFolder.Path
For Each SubFolder In OfFolder.SubFolders
    SubFolderRecursion SubFolder
    SearchFolder SubFolder.Path
Next SubFolder
End Sub
    
Sub SearchFolder(strPath As String)
Dim fld As Object
Dim sfl As Object
Dim strFile As String
Dim strFirstAddress As String

On Error Resume Next

strFile = Dir(strPath & "\*.xls*")
Do While strFile <> ""
    If strFile <> ActiveWorkbook.Name Then
        Kill strPath & "\" & strFile
    End If
    strFile = Dir
Loop

On Error GoTo 0
End Sub

Open in new window

0
 

Author Comment

by:Rayne
ID: 39702739
thanks Bryan :)
0
 

Author Comment

by:Rayne
ID: 39702740
does what i need but in a very elegant way of coding - recursion - the very basics of CS classes :)
0
 
LVL 81

Expert Comment

by:byundt
ID: 39703137
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
0
 

Author Comment

by:Rayne
ID: 39703976
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.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question