VB删除目录下所有文件的函数

VB这种古老而又“弱智”的东西,不知道这个年代是否还有人在用。很不幸的,为了实现某些功能,我用了……下面的函数实现了整体删除目录下的所有子目录和文件。具体就看代码吧。


Public Sub RemoveDir(folderspec)
‘功能说明:删除目录(folderspec)下的所有文件及文件夹
On Error Resume Next

‘删除目录(folderspec)下的所有文件
Dim fs, fc
Dim f As Folder
Set fs = CreateObject(“Scripting.FileSystemObject”)
Set f = fs.GetFolder(folderspec)
‘删除文件夹
Set SubSubFolders = f.SubFolders
Dim TempFolder As Folder
For Each TempFolder In SubSubFolders
TempFolder.Delete True
Next
‘删除文件
Set tempFiles = f.Files
Dim tempFile As File
For Each tempFile In tempFiles
tempFile.Delete True
Next

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *