Search Tools Links Login

Recursive Deltree Sub

Deletes a specified directory tree.

Original Author: Andy Ault


sFolder: Folder tree to remove (ie "T:TEMP")


Note that "Dir" doesn't work as you may expect with recursive programs. This code works around that problem by re-doing the Dir w/parameters after calling itself recursively.


No return. If you need to use this in situations where filesor folders might have read-only attributes or can't be deleted for other reasons, you will need to modify the code to add error handling and return an error code.

Side Effects

As always with this type of code, use it carefully. You could wipe valuable data.


Public Sub KillFolderTree(sFolder As String)
Dim sCurrFilename As String
sCurrFilename = Dir(sFolder & "*.*", vbDirectory)
Do While sCurrFilename <> ""
If sCurrFilename <> "." And sCurrFilename <> ".." Then
  If (GetAttr(sFolder & "" & sCurrFilename) And vbDirectory) = vbDirectory Then
  Call KillFolderTree(sFolder & "" & sCurrFilename)
  sCurrFilename = Dir(sFolder & "*.*", vbDirectory)
  On Error Resume Next
  Kill sFolder & "" & sCurrFilename
  On Error Goto 0
  sCurrFilename = Dir
  End If
  sCurrFilename = Dir
End If
On Error Resume Next
RmDir sFolder
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 84 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.