Send Files to the Recycle Bin
Skip Navigation Links.

Send Files to the Recycle Bin

Skip Navigation LinksHome :: Visual Basic :: Recycle Bin

Here's a simple Visual Basic function you can use in Vb6+/MS Access to send a file to the recycle bin.

Also requires: Directory related Constants to be declared in the same module and File Errors Handler

Public Function RecycleFile(FileName$) As Variant
'sends a file to the recycle bin
    Dim Msg As String
    Dim lReturn As Long
    Dim FileOperation As SHFILEOPSTRUCT
On Local Error GoTo RecycleFile_Err

    RecycleFile = False

                With FileOperation
                   .wFunc = FO_DELETE
                   .pFrom = FileName
                   .fFlags = FOF_ALLOWUNDO
                End With
                
                lReturn = SHFileOperation(FileOperation)
    
    If FileExists(FileName$) = True Then 'you cancelled
        Exit Function
    End If
    
   RecycleFile = True

RecycleFile_End:
   Exit Function

RecycleFile_Err:
    Select Case FileErrors
        Case Is = 0 ' = OK, Retry ,Yes.
            Resume 0
        Case Is = 1 ' = Ignore.
            Resume Next
        Case Is = 2 '= cancel,end
            Resume RecycleFile_End
        Case Is = 3 'other error, not file related
            Msg = "Error #: " & Format$(Err.Number) & vbCrLf
            Msg = Msg & Err.Description
            MsgBox Msg, vbInformation, "RecycleFile"
            Resume RecycleFile_End
    End Select

End Function

Copy and paste the Visual Basic code below into your module

Please Link to us Next >> Renaming Files
top of page
all content ©1996/2012 BennySutton.com
all images © their respective owners This site uses Thumbshots previews