Visual Basic Programming Code Examples Visual Basic > Files Directories Drives Code Examples Send files or folders-directories to the Recycle bin Send files or folders-directories to the Recycle bin 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 asDelete can be found. ' asDelete A 1d string array of file (or directory) names 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. Function RecycleBin(asDelete() 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 Delete String for an array of file names For lThisFile = LBound(asDelete) To UBound(asDelete) sFileNames = sFileNames & asDelete(lThisFile) & vbNullChar 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