by Andy Ault (1 Submission)
Category: Files/File Controls/Input/Output
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (7 Votes)
Deletes a specified directory tree.
Inputs
sFolder: Folder tree to remove (ie "T:\TEMP")
Assumes
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.
Code 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.
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