VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 删除目录和它的子目录及所有文件
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:13534, 日期:2001-04-09
Public Function KillFolder(ByVal FullPath As String) _
   As Boolean
   
`******************************************
`PURPOSE: DELETES A FOLDER, INCLUDING ALL SUB-
`         DIRECTORIES, FILES, REGARDLESS OF THEIR
`         ATTRIBUTES

`PARAMETER: FullPath = FullPath of Folder to Delete

`RETURNS:   True is successful, false otherwise

`REQUIRES:  `VB6
            `Reference to Microsoft Scripting Runtime
            `Caution in use for obvious reasons

`EXAMPLE:   `KillFolder("D:\MyOldFiles")

`******************************************
On Error Resume Next
Dim oFso As New Scripting.FileSystemObject

`deletefolder method does not like the "\"
`at end of fullpath

If Right(FullPath, 1) = "\" Then FullPath = _
    Left(FullPath, Len(FullPath) - 1)

If oFso.FolderExists(FullPath) Then
    
    `Setting the 2nd parameter to true
    `forces deletion of read-only files
    oFso.DeleteFolder FullPath, True
    
    KillFolder = Err.Number = 0 And _
      oFso.FolderExists(FullPath) = False
End If

End Function