CodeItBetter Programming Another VB Programming Blog

How to delete a file by moving it to the recycle bin

Posted on August 8, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'File/Folder Handling - How to delete a file by moving it to the recycle bin
Public 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_NOCONFIRMATION As Long = &H10
Public Const FOF_ALLOWUNDO As Long = &H40
 
Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
    (lpFileOp As SHFILEOPSTRUCT) As Long
 
'Returns True if successful
Public Function Erase2RecycleBin(fileSpec$) As Boolean
    Dim SHFileOp As SHFILEOPSTRUCT
    With SHFileOp
        .wFunc = FO_DELETE
        .pFrom = fileSpec$ & vbNullChar
        .fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION
    End With
    Erase2RecycleBin = (SHFileOperation(SHFileOp) = 0)
End Function
 
'Call the function like this:
'blnSuccess = Erase2RecycleBin("C:\Myfolder\myfile")
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.