Visual Basic Programming Code Examples
Visual Basic > Files Directories Drives Code Examples
Use the RecycleBin to delete files
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
Use the RecycleBin to delete files
'Make use of the RecycleBin
'make a new project with a form (name = frmDeleteFiles)
'put the code in the right places (use Insert/File)
'press F5
'on a module form
Option Explicit
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
Declare Function SHFileOperation Lib "shell32.dll" (lpFileOP as SHFILEOPSTRUCT) as Long
Type BrowseInfo
hWndOwner as Long
pIDLRoor as Long
pszDisplayName as Long
lpszTitle as Long
ulFlags as Long
lpfnCallback as Long
lParam as Long
iImage as Long
End Type
Public Const BIF_RETURNONLYFSDIRS = 1
Public Const MAX_PATH = 260
Public Declare sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem as Long)
Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 as String, ByVal lpString2 as String) as Long
Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi as BrowseInfo) as Long
Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList as Long, ByVal lpBuffer as String) as Long
Public Function blnDeleteFilesToRecycleBin(ParamArray vntFilename() as Variant) as Boolean
on Error GoTo ErrorToRecycleBin
dim intK as Integer
dim strFiles as String
dim udtShellFileOper as SHFILEOPSTRUCT
dim lngResult as Long
For intK = LBound(vntFilename) To UBound(vntFilename)
strFiles = strFiles & vntFilename(intK) & vbNullChar
Next
strFiles = strFiles & vbNullChar
With udtShellFileOper
.wFunc = &H3
.pFrom = strFiles
.fFlags = &H40
end With
lngResult = SHFileOperation(udtShellFileOper)
blnDeleteFilesToRecycleBin = True
exit Function
ErrorToRecycleBin:
blnDeleteFilesToRecycleBin = False
exit Function
End Function
Public Function strChooseFolder(hWndOwner as Long, strPrompt as String) as String
dim intNull as Integer
dim lngIDList as Long
dim lngResult as Long
dim strPath as String
dim udtBI as BrowseInfo
With udtBI
.hWndOwner = hWndOwner
.lpszTitle = lstrcat(strPrompt, "")
.ulFlags = BIF_RETURNONLYFSDIRS
end With
lngIDList = SHBrowseForFolder(udtBI)
If lngIDList Then
strPath = String$(MAX_PATH, 0)
lngResult = SHGetPathFromIDList(lngIDList, strPath)
Call CoTaskMemFree(lngIDList)
intNull = InStr(strPath, vbNullChar)
If intNull Then
strPath = Left$(strPath, intNull - 1)
end If
end If
strChooseFolder = strPath
End Function
'on the form called frmDeleteFiles
Private sub Form_Load()
dim strPath as String
dim blnResult as Boolean
strPath = strChooseFolder(Me.hWnd, "Choose a folder")
If Not blnDeleteFilesToRecycleBin(strPath & "\*.url") Then MsgBox "error"
Unload frmDeleteFiles
End Sub
Private sub Form_Unload(Cancel as Integer)
End
End Sub
Return