Created by Jim Rech:
Removes a known directory including all of its files and any/all
possible sub-directories of unknown quantity & name/s including their
files.
Const FO_DELETE = &h3&
Const FOF_NOCONFIRMATION = &h10&
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String
End Type
Private Declare Sub CopyMemory Lib “KERNEL32” Alias “RtlMoveMemory”
(hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function SHFileOperation Lib “Shell32.dll” Alias
“SHFileOperationA” (lpFileOp As Any) As Long
Sub Test()
ShellDelete “c:aaa”
End Sub
Sub ShellDelete(SrcFile As String)
Dim result As Long
Dim lenFileop As Long
Dim foBuf() As Integer
Dim fileop As SHFILEOPSTRUCT
lenFileop = LenB(fileop)
ReDim foBuf(1 To lenFileop)
With fileop
.hwnd = 0
.wFunc = FO_DELETE
.pFrom = SrcFile & Chr(0) & Chr(0)
.fFlags = FOF_NOCONFIRMATION
.lpszProgressTitle = “” & Chr(0) & Chr(0)
End With
Call CopyMemory(foBuf(1), fileop, lenFileop)
Call CopyMemory(foBuf(19), foBuf(21), 12)
result = SHFileOperation(foBuf(1))
End Sub
Const FOF_NOCONFIRMATION = &h10&
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String
End Type
Private Declare Sub CopyMemory Lib “KERNEL32” Alias “RtlMoveMemory”
(hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function SHFileOperation Lib “Shell32.dll” Alias
“SHFileOperationA” (lpFileOp As Any) As Long
Sub Test()
ShellDelete “c:aaa”
End Sub
Sub ShellDelete(SrcFile As String)
Dim result As Long
Dim lenFileop As Long
Dim foBuf() As Integer
Dim fileop As SHFILEOPSTRUCT
lenFileop = LenB(fileop)
ReDim foBuf(1 To lenFileop)
With fileop
.hwnd = 0
.wFunc = FO_DELETE
.pFrom = SrcFile & Chr(0) & Chr(0)
.fFlags = FOF_NOCONFIRMATION
.lpszProgressTitle = “” & Chr(0) & Chr(0)
End With
Call CopyMemory(foBuf(1), fileop, lenFileop)
Call CopyMemory(foBuf(19), foBuf(21), 12)
result = SHFileOperation(foBuf(1))
End Sub
Editor’s note: Tested and it works, but I still don’t get it. I’m going to print it out and put it under my pillow for a couple of nights and see if that helps. In the mean time, you can leave a comment explaining it to me.
This MSDN page describes the SHFileOperation Function:
SHFileOperation Function
>>Created by Jim Rech:
I don’t remember creating this but if I did it was only the Sub Test part
Ya mean that neat compact code does the same thing as all the mumbo jumbo below?
CreateObject(“scripting.filesystemobject”).DeleteFolder “c: emp estdelete”, True