Search Tools Links Login

Recursive Deltree Sub


Deletes a specified directory tree.

Original Author: Andy Ault

Inputs

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

Assumptions

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.

Returns

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.

Code

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)
  Else
  On Error Resume Next
  Kill sFolder & "" & sCurrFilename
  On Error Goto 0
  sCurrFilename = Dir
  End If
Else
  sCurrFilename = Dir
End If
Loop
On Error Resume Next
RmDir sFolder
End Sub

About this post

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

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.