Sub 删除所有空文件夹()
Dim strpath As String
strpath = Application.InputBox(prompt:="请输入文件夹,名称:", _
Title:="输入文件夹名称", Default:=thisworbook.Path, Type:=2)
If strpath = "False" Or strpath = "" Then Exit Sub
Call delemtydir(strpath)
End Sub
Sub delemtydir(strpath)
Dim fso As New Scripting.FileSystemObject
Dim strdirname As String
Dim lastdir As String
Dim strfld As String
Dim fld As Folder
If strpath = "False" Or strpath = "" Then Exit Sub
If Right(strpath, 1) <> "\" Then strpath = strpath & "\"
strdirname = Dir(strpath, vbDirectory)
Do While strdirname <> ""
If strdirname <> "." And strdirname <> ".." Then
If (GetAttr(strpath & strdirname) And vbDirectory) = vbDirectory Then
lastdir = strdirname
Set fld = fso.GetFolder(strpath & strdirname)
If fld.Size = 0 Then
fld.Delete
strfld = Left(strpath & strdirname, InStrRev(strpath$ & strdirname, "\") - 1)
Call delemtydir(strfld)
Else
Call delemtydir(strpath & strdirname)
End If
strdirname = Dir(strpath, vbDirectory)
Do Until strdirname = lastdir Or strdirname = ""
strdirname = Dir
Loop
If strdirname = "" Then Exit Sub
End If
End If
strdirname = Dir
Loop
Set fso = Nothing
End Sub
标签:Dim,End,Sub,删除,所有,文件夹,delemtydir,strpath,strdirname
From: https://blog.csdn.net/qq490525427/article/details/139552936