怎样用VBA删除根目录下的所有文件及文件夹

比如把在D盘下的文件全部删除。

请小心使用以下代码!!后果自负
Sub DelAll()
strpathname = "D:\"
CreateObject("scripting.filesystemobject").getfolder(strpathname).Delete True
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-07-21
Sub Test()
EnumFiles ("D:")
End Sub

Sub EnumFiles(ByVal sPath As String)
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
GetFile fso.getfolder(sPath)
Set fso = Nothing
End Sub

Sub GetFile(ByVal fldParent As Folder)
Dim fldSub As Folder, fSub As File
For Each fldSub In fldParent.SubFolders
fldSub.Delete True
Next
For Each fSub In fldParent.Files
fSub.Delete True
Next
Set fldSub = Nothing
Set fSub = Nothing
End Sub
相似回答