Kill folders in a directory (windows 7) using excel vba

Posted on 2014-08-22
Last Modified: 2014-08-23
Dear Experts:

I got hundreds of JPG folders in the following Windows 7 directory C:\\MyPictures\\...
These jpg folders can be nested in different levels in this directory such as:

C:\\MyPictures\\Vacation\\JPG\\... or
C:\\MyPictures\\Vacation\\2014\\JPG\\... or

So wherever these JPG-Folders are located they are to be deleted along with any subfolders which may be nested below.

Help is much appreciated.

Thank you very much in advance

Regards, Andreas
Question by:AndreasHermle
    LVL 47

    Accepted Solution


    Doing some maintenance?

    pls try with caution ( with reference to Microsoft Scripting runtime )

    Sub DeleteJpgFolder()
    Dim FSO As Object
    Set FSO = CreateObject("scripting.filesystemobject")
    strPath = "C:\MyPictures\Vacation"
    If Right(strPath, 1) = "\" Then
        strPath = Left(strPath, Len(strPath) - 1)
    End If
    Res = fDeleteJpgFolder(FSO, strPath)
    End Sub
    Function fDeleteJpgFolder(ByRef oFSO As FileSystemObject, ByVal strSubFolder)
    Set oFolder = oFSO.GetFolder(strSubFolder)
    If oFolder.Name = "JPG" Then
        oFSO.DeleteFolder oFolder.Path
        Set oSFolders = oFolder.SubFolders
        For Each oSFolder In oSFolders
            StrSPath = oSFolder.Path
            fDeleteJpgFolder oFSO, StrSPath
    End If
    End Function

    Open in new window

    EDIT Correct delete folder line


    Author Closing Comment

    Hi rgonzo you saved my day thank you very much :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
    What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
    This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
    This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now