VB6 - Moving Files to the Recycle Bin
When a file is deleted in windows, it is not automatically erased. Instead, it is put into the recycle bin. Thus, an unwanted deletion can easily be undone. You can do this in your programs with these codes;
VB Code:
' Copy this code into a module
' You can add a module by right clicking in the project
' explorer of vb
Public Type SHFILEOPSTRUCT
hwnd As LongwFunc As
LongpFrom As StringpTo As Stringf
Flags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As Long
End Type
Public Declare Function SHFileOperation Lib _"shell32.dll" _ Alias "SHFileOperationA" (lpFileOp _As SHFILEOPSTRUCT) As Long
Public Const FO_DELETE = &H3
Public Const FOF_ALLOWUNDO = &H40
Public Function Move2RecycleBin(strfile as String)
'strfile is the full path of the file you want to put in the recycle bin
Dim SHop As SHFILEOPSTRUCT
With SHop
.wFunc = FO_DELETE
.pFrom = strFile
.fFlags = FOF_ALLOWUNDO
End With
SHFileOperation SHop
End Function
'Sample Usage
Private Sub Form1_Activate()
Move2RecycleBin("C:\PutMe.txt")
End Sub
'Code By : Rigie O. Acebedo
Re: VB6 - Moving Files to the Recycle Bin
You should check for the possibility of the user Canceling the dialog. You do that by looking at If FileOperation.fAnyOperationsAborted which will be True in that case.
Re: VB6 - Moving Files to the Recycle Bin
For an update as of August, 2024 under Windows 10 the function provided still works perfectly.
It works with reasonable speed but not as quickly as the simple "NAME" command that I previously used when saving files that might be needed later or "accidentally" erased.
My present program is working with tens of thousands of files of wildly varying size and multiple format so speed is actually becoming significant factor and I'm employing a virtual disc for the first time since I used them (in "extended" memory) to store simple text screen images enabling effectively instant screen changes.