Use recursive algorithms to delete directories with multi-level subdirectories
Option Explicit
Private Sub commandementclick ()
Dim strPathName As String
StrPathName = ""
StrPathName = InputBox ("enter the name of the folder to be deleted:", "delete folder ")
If strPathName = "" Then Exit Sub
On Error GoTo ErrorHandle
SetAttr strPathName, vbNormal this line is mainly used to check the validity of the folder name
RecurseTree strPathName
Label1.Caption = "folder" & strPathName & "deleted! "
Exit Sub
ErrorHandle:
MsgBox "invalid folder name:" & strPathName
End Sub
Sub RecurseTree (CurrPath As String)
Dim sFileName As String
Dim newPath As String
Dim sPath As String
Static oldPath As String
SPath = CurrPath &""
SFileName = Dir (sPath, 31) '31 meaning: 31 = vbNormal + vbReadOnly + vbHidden + vbSystem + vbVolume + vbDirectory
Do While sFileName <> ""
If sFileName <> "." And sFileName <> ".." Then
If GetAttr (sPath & sFileName) And vbDirectory Then 'is a directory And folder
NewPath = sPath & sFileName
RecurseTree newPath
SFileName = Dir (sPath, 31)
Else
SetAttr sPath & sFileName, vbNormal