Link to home
Start Free TrialLog in
Avatar of Andreas Hermle
Andreas HermleFlag for Germany

asked on

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
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

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
ASKER CERTIFIED SOLUTION
Avatar of [ fanpages ]
[ fanpages ]

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Andreas Hermle

ASKER

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
@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.
Avatar of [ fanpages ]
[ fanpages ]

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.
@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 https://www.experts-exchange.com/questions/28694250/By-Ref-and-ByVal-explained-in-plain-examples.html

thanks.