Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 156
  • Last Modified:

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

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
1 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

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now