VB 删除带子文件夹和文件的文件夹

时间:2024-10-30 07:16:25
Sub RecurseTree(CurrPath As String) 'currpath问文件夹路径
    Dim sFileName As String, newPath As String, sPath As String
    Static oldPath As String
    sPath = CurrPath & "\"
    sFileName = Dir(sPath, 31) '31的含义∶31=vbNormal+vbReadOnly+vbHidden+vbSystem+vbVolume+vbDirectory
    Do While sFileName <> ""
        If sFileName <> "." And sFileName <> ".." Then
            If GetAttr(sPath & sFileName) And vbDirectory Then '如果是目录和文件夹
                newPath = sPath & sFileName
                RecurseTree newPath
                sFileName = Dir(sPath, 31)
            Else
                SetAttr sPath & sFileName, vbNormal
                Kill (sPath & sFileName)
                ' = sPath & sFileName '显示删除过程
                sFileName = Dir
            End If
        Else
            sFileName = Dir
        End If
        DoEvents
    Loop
    SetAttr CurrPath, vbNormal
    RmDir CurrPath
    ' = CurrPath
End Sub