Option Compare Database
Option Explicit
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Public Const FO_DELETE As Long = &H3
Public Const FOF_ALLOWUNDO As Long = &H40
Private Declare Function SHFileOperation Lib "shell32" _
Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
Public Function ShellDelete(sFile As String, Optional FOF_FLAGS As Long = FO_DELETE) As Boolean
On Error GoTo Err_Delete
Dim shf As SHFILEOPSTRUCT
Dim lngMsg As Long
'Rimuove il terminatore di stringa "\" inserendo
'2 caratteri nulli
If Mid$(sFile, Len(sFile), 1) = "\" Then sFile = Mid$(sFile, 1, Len(sFile) - 1)
sFile = sFile & vbNullChar & vbNullChar
'Imposta la variabile da passare alla funzione API
'per la cancellazione
With shf
.wFunc = FO_DELETE 'Tipo di azione da eseguire
.pFrom = sFile 'File/Folder sulla quale eseguirla
.fFlags = FOF_FLAGS 'Flags speciali(come FOF_ALLOWUNDO)
End With
'Esegue l'istruzione e ne recupera l'esito
'la variabile è passata ByRef e viene completata
Call SHFileOperation(shf)
ShellDelete = Not shf.fAborted
Exit Function
Err_Delete:
ShellDelete = False
End Function
|