In VBA Developer's Handbook, in Chapter 12, the dhDelTree procedure has a serious issue if you call it passing a path referring to a drive other than the current drive. In that case, it deletes files from the current drive, not the specified drive. This can lead to serious data loss.
The following replacement function switches drive and directory to the specified location, so that it correctly deletes files from the specified path. Do
not use the dhDelTree function in the book -- make sure you replace all copies of that function with this one, to avoid possible data loss.
Neither function will work for you unless you have the rest of the procedures in the chapter, so there's no point downloading these fixes unless you have the
book.
Private Declare Function SetCurrentDirectory _
Lib "kernel32" Alias "SetCurrentDirectoryA" _
(ByVal lpPathName As String) As Long
Function dhDelTree(ByVal Directory As String, _
Optional RemoveRoot As Boolean = True, _
Optional ByVal Level As Integer = 1) As Boolean
On Error GoTo HandleErrors
Dim strFilename As String
Dim strDirectory As String
strDirectory = dhFixPath(Directory)
If Len(Dir(strDirectory, vbDirectory)) = 0 Then
GoTo ExitHere
End If
If dhFixPath(CurDir) = strDirectory Then
MsgBox "Unable to delete the current directory. " & _
"Move to a different directory, and try again."
GoTo ExitHere
End If
strFilename = Dir(strDirectory & "*.*")
Do Until strFilename = ""
Kill strDirectory & strFilename
strFilename = Dir
Loop
Do
strFilename = Dir(strDirectory & "*.*", vbDirectory)
Do While strFilename = "." Or strFilename = ".."
strFilename = Dir
Loop
If strFilename = "" Then
Exit Do
Else
If Not dhDelTree(strDirectory & strFilename, True, Level + 1) Then
GoTo ExitHere
End If
End If
Loop
If Level = 1 Imp RemoveRoot Then
RmDir strDirectory
End If
dhDelTree = True
ExitHere:
Exit Function
HandleErrors:
Select Case Err.Number
Case 75
Resume Next
Case Else
dhDelTree = False
MsgBox Err.Description, vbExclamation, _
"Error " & Err.Number & " in dhDelTree"
Resume ExitHere
End Select
End Function
In VBA Developer's Handbook, in Chapter 12, if you use the dhDir function to visit folders, the function doesn't correctly release a resource referring to that folder, and you won't be able to delete the folder without shutting down VBA (by quitting the host application). The following replacement for
dhDir solves the problem.
Function dhDir(Optional ByVal strPath As String = "", _
Optional lngAttributes As Long = vbNormal, _
Optional fExclusive As Boolean = True) As String
Dim fd As WIN32_FIND_DATA
Static hFind As Long
Static lngAttr As Long
Static fEx As Boolean
Dim strOut As String
If strPath = "" Then
If hFind > 0 Then
If CBool(FindNextFile(hFind, fd)) Then
strOut = dhFindByAttr(hFind, fd, lngAttr, fEx)
End If
End If
Else
lngAttr = lngAttributes
fEx = fExclusive
If Right(strPath, 1) = "\" Then
strPath = strPath & "*.*"
End If
hFind = FindFirstFile(strPath, fd)
If hFind > 0 Then
strOut = dhFindByAttr(hFind, fd, lngAttr, fEx)
End If
End If
If Len(strOut) = 0 Then
If hFind > 0 Then
Call FindClose(hFind)
End If
End If
dhDir = strOut
End Function
|