VB and VBA Users Source Code: Send files or folders/directories to the Recycle bin
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Send files or folders/directories to the Recycle bin
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Sunday, October 29, 2000
Hits:
618
Category:
Windows API
Article:
To send files to the recycle bin use the following routine (a demonstration routine is at the bottom of this post): Option Explicit Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long Private Type SHFILEOPSTRUCT hwnd As Long Func As Long From As String To As String Flags As Integer Aborted As Boolean NameMaps As Long Progress As String End Type 'Purpose : Sends a directory or array of files names to the recycle bin 'Inputs : sPath The path of the directory to delete or the location ' where the files in asFilesToDelete can be found. ' asFilesToDelete A 1d string array of file names (or directories) to send to the recycle bin ' [lHwnd] A handle to the form calling this function ' [bShowAlert] If True displays a confirmation msgbox, else doesn't 'Outputs : Returns True if the user did not cancel the delete. Note that the user may ' have canceled individual file deletes. 'Author : Andrew Baker 'Date : 30/10/2000 01:09 'Notes : 'Revisions : Function RecycleBin(asFilesToDelete() As String, Optional lHwnd As Long, Optional bShowAlert As Boolean = True) As Boolean Dim lThisFile As Long, sFileNames As String, lRetVal As Long Dim SHFileOp As SHFILEOPSTRUCT, sFullPathName As String Const FO_DELETE = &H3 Const FOF_ALLOWUNDO = &H40 Const FOF_NOCONFIRMATION = &H10 ' Don't prompt the user. On Error GoTo ErrFailed 'Build up a delimited list of files to delete For lThisFile = LBound(asFilesToDelete) To UBound(asFilesToDelete) If Len(asFilesToDelete(lThisFile)) > 0 Then sFileNames = sFileNames & asFilesToDelete(lThisFile) & vbNullChar End If Next If Len(sFileNames) Then With SHFileOp .Func = FO_DELETE .From = sFileNames .Flags = FOF_ALLOWUNDO If bShowAlert = False Then .Flags = .Flags Or FOF_NOCONFIRMATION End If .hwnd = lHwnd End With lRetVal = SHFileOperation(SHFileOp) If SHFileOp.Aborted = False Then 'Succeeded RecycleBin = True Else 'Aborted RecycleBin = False End If End If Exit Function ErrFailed: Debug.Print Err.Description Debug.Assert False 'Failed RecycleBin = False Exit Function Resume End Function 'Demonstration routine Sub test() Dim asFiles(1 To 1) As String asFiles(1) = "C:\setup.exe" Call RecycleBin(asFiles, 0, False) End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder