View Full Version : How to delete a file using VB5


smiler44
01-30-2010, 12:52 PM
I want to delete a file and send it to to the recycle bin. Currently I seem to be able to kill/delete a file but the files do not go to the recycle bin and so can I not recover them if I make an error.

Thank you in advance.

smiler44

georgedwilkinson
01-30-2010, 01:24 PM
Try this:
http://vbadud.blogspot.com/2008/08/vba-send-file-to-recycle-bin.html

Google is your friend.

smiler44
01-30-2010, 02:53 PM
George,
Thank you. just tred copying the ccode from the web page and it errors (turns red) in a couple of places.

should the code go in a module or somewhere specifec?


Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
()
(ByRef lpFileOp As SHFILEOPSTRUCT) As Long


and at

Err.Clear()

georgedwilkinson
01-30-2010, 03:13 PM
Replace:
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
()
(ByRef lpFileOp As SHFILEOPSTRUCT) As Long

With:
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Long

Replace:
Err.Clear()

With:
Err.Clear

georgedwilkinson
01-30-2010, 03:16 PM
Oh, and yes, it should be in a module.

smiler44
01-30-2010, 03:41 PM
I'll possibly give it another go but taking a look at one of my other posts and the advice given about saving a file to a different directory I guess this would do as an easy way to "delete a file" and send it to the recycle bin. need to change "c:\temp\temp2\" for the path of the recycle bin. Just need to try it but :


Sub Command4_Click()
Dim OldName, NewName
Dim pth As String
Dim pthh As String
Dim oldfile As String


pth = "c:\temp\"
pthh = "c:\temp\temp2\"
oldfile = "gym record.xls"
OldName = pth & oldfile: NewName = pthh & oldfile
Name OldName As NewName ' moves file?

End Sub

georgedwilkinson
01-30-2010, 03:49 PM
I tried that approach before responding the first time. On Vista, the file won't go directly into the Recycle Bin but ends up in a weird place (c:\$Recycle\).

smiler44
01-31-2010, 02:59 AM
mmmm a search on google and moving the files to the recycle bin is not so easy so I've cheated. I've created a new directory and will move the unwanted files into that and then can easily delete them from there. At last I've got the code to work and it moved the unwanted files in a fraction of a second. Currently I have to manaully change the directory name program has to look in to find the files to delete/move. My next task will be to make it search through the subdirectories automaticlly to save me manually changing the name. I will then move on to the next problem... sorry enhancement.

smiler44

georgedwilkinson
01-31-2010, 11:54 AM
That sounds like a reasonable approach.

ghudson
02-01-2010, 08:31 AM
SHFileOperation: Add Recycle Bin Functionality (http://vbnet.mvps.org/index.html?code/shell/recyclebin.htm)

anthonypia
02-09-2010, 03:05 AM
Nice and Great help

Thanks Fpr this nice and Great information. I agree with you.