Results 1 to 3 of 3

Thread: Removing empty directories

  1. #1
    Guest
    I'm using the following code that I found on this site to remove ONLY empty directories. Does anyone have a better routine than this one? Sometimes the procedure is finished within a second, sometimes it takes 15+ seconds, sometimes it never finishes (gets in an infinite loop).

    Thanks in advance,
    Dave

  2. #2
    Guest
    Sorry, here's the code:

    Public Sub remove_folders(root_dir As String, Optional recursive As Long)
    On Error Resume Next
    Dim idx As Long
    Dim upper_limit As Long
    Dim sub_folder() As String

    If IsMissing(recursive) = True Then
    recursive = 0
    End If

    ReDim sub_folder(idx)
    sub_folder(idx) = Dir(root_dir, vbDirectory Or vbNormal Or vbHidden Or vbReadOnly Or vbSystem)

    Do While sub_folder(idx) <> vbNullString
    idx = idx + 1
    ReDim Preserve sub_folder(idx)
    sub_folder(idx) = Dir
    Loop

    If idx <= 2 Then
    Call RmDir(root_dir)
    'Only deletes if directory is empty.
    DoEvents
    Else
    upper_limit = UBound(sub_folder) ' - 1
    For idx = 0 To upper_limit Step 1
    If GetAttr(root_dir & sub_folder(idx)) = vbDirectory _
    And sub_folder(idx) <> "." _
    And sub_folder(idx) <> ".." Then
    Call remove_folders(root_dir & sub_folder(idx) & "\", recursive + 1)
    End If
    Next idx
    End If

    Erase sub_folder
    End Sub

  3. #3
    Guest
    Don't worry about it, I've coded my own routine that works great.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width