How to delete a file using VB5

smiler44

Registered User.
Local time
Today, 10:28
Joined
Jul 15, 2008
Messages
671
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
 
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?

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

and at
Code:
Err.Clear()
 
Replace:
Code:
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" _
()
(ByRef lpFileOp As SHFILEOPSTRUCT) As Long

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

Replace:
Code:
Err.Clear()

With:
Code:
Err.Clear
 
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 :

Code:
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
 
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\).
 
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
 
Nice and Great help

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

Users who are viewing this thread

Back
Top Bottom