Delete multiple subfolders in Windows Explorer using Excel VBA (Excel 2010 running on Windows 7 Enterprise)

Dear Experts:

I would like to delete all JPEG-subfolders which are nested under C:\Graphics\.... using a VBA code run from Excel 2010 (Windows 7) such as ...

C:\Graphics\10\10-150\10-150-04-07\JPEG or
C:\Graphics\11\11-243\11-243-05-09\JPEG
etc.

Professional Help is very much appreciated. Thank you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can use the following code..It will delete all the subfolders from the path c:graphics..


Sub deletefolders()
    Dim fo As Object
    Dim fl As Object
    Dim sfl As Object


    Set fo = CreateObject("Scripting.FileSystemObject")

    Set fl = fo.GetFolder("C:\Graphics\")


    For Each sfl In fl.subfolders

        sfl.Delete

    Next sfl
End Sub
0
[ fanpages ]IT Services ConsultantCommented:
Hi Andreas,

To expand upon the previous suggestion, & actually answering your question:

Option Explicit
Public Sub Delete_JPEG_Folders()

  Dim objFolder                                         As Object
  
  For Each objFolder In CreateObject("Scripting.FileSystemObject").GetFolder("C:\Graphics").SubFolders
  
      If objFolder.SubFolders.Count = 0& Then
         If UCase$(objFolder.Name) = "JPEG" Then
            ChDir objFolder.ParentFolder.Path
            objFolder.Delete
         End If
      Else
         Call Delete_JPEG_SubFolders(objFolder)
      End If
        
  Next objFolder
  
  Set objFolder = Nothing
  
End Sub
Public Sub Delete_JPEG_SubFolders(ByRef objFolder As Object)

  Dim objSubFolder                                      As Object
  
  For Each objSubFolder In objFolder.SubFolders
   
      If objSubFolder.SubFolders.Count = 0& Then
         If UCase$(objSubFolder.Name) = "JPEG" Then
            ChDir objSubFolder.ParentFolder.Path
            objSubFolder.Delete
         End If
      Else
         Call Delete_JPEG_SubFolders(objSubFolder)
      End If
      
  Next objSubFolder
  
  Set objSubFolder = Nothing
  
End Sub

Open in new window



Usage:

Call Delete_JPEG_Folders()
2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andreas HermleTeam leaderAuthor Commented:
Wow fanpages, I am deeply impressed, this saves me hundreds of hours.

Thank you very much for your great and professional help.  I really appreciate it. Regards, Andreas
1
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

FloraCommented:
@Fanpages

may you please explain which both of your subs are identical and what is the secret of using "ByRef" here?  i would like to learn, why your are calling exactly same SUB  inside each other
i only see one difference between two codes,  OBj.folder and obj.subfolder
i would appreciate if you could comment the code, so that it is understable.

thanks alot.
0
[ fanpages ]IT Services ConsultantCommented:
You're very welcome, Andreas.  Thanks for closing the question so quickly too.

@Flora:

This isn't really the place to ask questions, but I'll try not to be rude (if Andreas does not mind)! :)

The two routines are not identical; the first ("Delete_JPEG_Folders()") starts the process at the highest folder in the hierarchy ("C:\Graphics") looking for all sub-folders from this folder.

The second routine, "Delete_JPEG_SubFolders(...)", "does what it say on the tin"; it iterates through all the sub-folders of the Folder object passed as a parameter, looking for the lowest level folder with the explicit name of "JPEG".

Delete_JPEG_Folders(...) can call itself as sub-folders of the top level folder, can also have sub-folders, & in turn, further sub-folders, & so on.

Passing a parameter ByRef (By Reference, rather than By Value) is not a secret; it is used as I wish to refer to the contents of the object set outside of the call to the "Delete_JPEG_Folders(...)" subroutine.

Please research the difference between ByRef & ByVal, & then come back to me if it is still unclear why I am using this syntax.

It will probably be easier if you start a new question thread, though.
0
FloraCommented:
@fanpages

thank you very much indeed.

you are right, i should have created a new question.  i still do not understand by simple example how these two ByRef & ByVal are different from each other and what is use for each of them.

i opened a question link is here http://www.experts-exchange.com/questions/28694250/By-Ref-and-ByVal-explained-in-plain-examples.html

thanks.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.