Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to move files to the Recycle Bin 1

Status
Not open for further replies.

bitbrain

Programmer
Jun 10, 1999
159
US
I have a VB5 program that deletes files from disk via the "kill" statement - everything works fine.<br>
<br>
Is there a way of moving the files to the Recycle Bin instead? It would be nice if the user had the option of recovering deleted files.<br>
<br>
Thanks in advance!
 
bitbrain,<br>
<br>
Think you'll have to write code to move the file(s) from current location to the recycle bin. (There probably is an API call for this, but I don't know it off the top of my head, and Appleman's reference is at work at the moment.)<br>
<br>
Also, you might check the MSDN site at Microsoft - I've seen several references - or at least inferences - there that could help.<br>
<br>
If you don't have any luck by Tuesday (Sep 7, 1999 - I'm on the US holiday schedule &lt;grin!&gt;), give us a yell and we'll see ab. fleshing out the bones.<br>
<br>
Make a good day . . .<br>
. . . barn<br>

 
Try putting the following in a module:<br>
<br>
Option Explicit<br>
<br>
Type SHFILEOPSTRUCT<br>
hwnd As Long<br>
wFunc As Long<br>
pFrom As String<br>
pTo As String<br>
fFlags As Integer<br>
fAnyOperationsAborted As Long<br>
hNameMappings As Long<br>
lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS<br>
End Type<br>
<br>
Public Const FO_DELETE = &H3<br>
Public Const FOF_ALLOWUNDO = &H40<br>
<br>
Declare Function SHFileOperation _<br>
Lib "shell32.dll" _<br>
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long<br>
<br>
<br>
You should then be able to call the ShellDelete function as follows:<br>
<br>
If KeyCode = 46 Then 'user pressed DEL<br>
ShellDelete (File1.Path & "\" & File1.FileName)<br>
End If<br>
<br>
Good luck Bruce
 
Thanks to both of you. I'll try the API call and see how it works out.
 
I got the API call to work and am on the right track - thanks to your help. I want the program to run behind the scenes without user interation. In order to do this I need to use the FOF_NOCONFIRMATION flag. Where can I find the value for this constant? <br>
<br>
By the way, the code for the ShellDelete function follows:<br>
<br>
<br>
Public Function ShellDelete(ParamArray vntFileName() As Variant)<br>
Dim I As Integer<br>
Dim sFileNames As String<br>
Dim SHFileOp As SHFILEOPSTRUCT<br>
For I = LBound(vntFileName) To UBound(vntFileName)<br>
sFileNames = sFileNames & vntFileName(I) & vbNullChar<br>
Next<br>
sFileNames = sFileNames & vbNullChar<br>
With SHFileOp<br>
.wFunc = FO_DELETE<br>
.pFrom = sFileNames<br>
.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION<br>
End With<br>
ShellDelete = SHFileOperation(SHFileOp)<br>
End Function<br>

 
'Add a module to your project (In the menu choose Project -&gt; Add Module, Then click Open)<br>
'If the user windows 'delete to recycle bin' option is disabled, the file will deleted completely.<br>
'Insert this code to the module :<br>
<br>
Public Type SHFILEOPSTRUCT<br>
hwnd As Long<br>
wFunc As Long<br>
pFrom As String<br>
pTo As String<br>
fFlags As Integer<br>
fAnyOperationsAborted As Boolean<br>
hNameMappings As Long<br>
lpszProgressTitle As String<br>
End Type<br>
Declare Function SHFileOperation Lib &quot;shell32.dll&quot; Alias &quot;SHFileOperationA&quot; _<br>
(lpFileOp As SHFILEOPSTRUCT) As Long<br>
Public Const F0_DELETE = &H3<br>
Public Const F0F_ALLOWUNDO = &H40<br>
Public Const F0F_CREATEPROGRESSDLG As Long = &H0<br>
<br>
'Insert the following code to your form:<br>
<br>
Private Sub Form_Load()<br>
Dim MyBool As Boolean<br>
'Replace 'c:\MyDir\MyFile.exe' with the name of the file you want to delete.<br>
DelToRecycBin (&quot;c:\MyDir\MyFile.exe&quot;)<br>
End Sub<br>
<br>
Public Function DelToRecycBin(FileName As String)<br>
Dim FileOperation As SHFILEOPSTRUCT<br>
Dim lReturn As Long<br>
On Error GoTo DelToRecycBin_Err<br>
With FileOperation<br>
.wFunc = F0_DELETE<br>
.pFrom = FileName<br>
.fFlags = F0F_ALLOWUNDO + F0F_CREATEPROGRESSDLG<br>
End With<br>
lReturn = SHFileOperation(FileOperation)<br>
Exit Function<br>
DelToRecycBin_Err:<br>
MsgBox Err.Description<br>
End Function<br>
<br>
<p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Basic Center</a><br>
 
I just found this thread with a keyword search and noticed the question was not completely answered...

I googled "FOF_NOCONFIRMATION" and got a list of delcaration matches which included

FOF_NOCONFIRMATION = 16

and

FOF_NOCONFIRMATION = &H10

(which is the same thing)

Here is a better list of constants:
Private Const FO_MOVE = 1
Private Const FO_COPY = 2
Private Const FO_DELETE = 3
Private Const FO_RENAME = 4

Private Const FOF_MULTIDESTFILES = &H1&
Private Const FOF_CONFIRMMOUSE = &H2&
Private Const FOF_SILENT = &H4&
Private Const FOF_RENAMEONCOLLISION = &H8&
Private Const FOF_NOCONFIRMATION = &H10&
Private Const FOF_WANTMAPPINGHANDLE = &H20&
Private Const FOF_ALLOWUNDO = &H40&
Private Const FOF_FILESONLY = &H80&
Private Const FOF_SIMPLEPROGRESS = &H100&
Private Const FOF_NOCONFIRMMKDIR = &H200&
Private Const FOF_NOERRORUI = &H400&
Private Const FOF_NOCOPYSECURITYATTRIBS = &H800&
Private Const FOF_NORECURSION = &H1000&
Private Const FOF_NO_CONNECTED_ELEMENTS = &H2000&
Private Const FOF_WANTNUKEWARNING = &H4000&
Private Const FOF_NORECURSEREPARSE = &H8000&

(ref:
This might make it easier in the future for people (like myself) to find this information when they search the forum

Thanks,
Josh

Have Fun, Be Young... Code BASIC
-Josh


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top