vba remvoving Directory : <p>Have a bit of a brain teaser.
How do you delete a directory with all its contents, including all sub folders, all files, etc.</p>

<p>I have the following code that has a recursive call, but it only deletes leaf folders.</p>

<p>It deletes all files and it deletes leaf folders (last nodes).</p>

<p>Anybody know of a way to kill one whole path in VBA, including all files and folders?</p>

<p>Maybe there is an easier way than recursion....
Thanks...</p>

<pre><code>Sub DeleteFiles()


    Call RecursiveFolderDelete("C:\tmp1")

    'RmDir ("C:\tmp1")


End Sub

Sub RecursiveFolderDelete(MyPath As String)

    Dim FileSys As FileSystemObject
    Dim objFolder As Folder
    Dim objSubFolder As Folder
    Dim objFile As File

    'On Error GoTo 0

    Set FileSys = CreateObject("Scripting.FileSystemObject")
    Set objFolder = FileSys.GetFolder(MyPath)

    For Each objFile In objFolder.Files
        If Left(objFile.Name, 1) &lt;&gt; "~" And objFile.Name &lt;&gt; ThisWorkbook.Name Then
            objFile.Delete
        End If
    Next objFile


    Dim Count As Integer
    Count = 0
    For Each objSubFolder In objFolder.SubFolders
        Count = Count + 1
        RecursiveFolderDelete MyPath &amp; "\" &amp; objSubFolder.Name
    Next objSubFolder
    If Count = 0 Then
        RmDir MyPath
    End If


    Set FileSys = Nothing
    Set objFolder = Nothing
    Set objSubFolder = Nothing
    Set objFile = Nothing

End Sub
</code></pre>
