Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jul 2002
    Location
    Sacramento, CA
    Posts
    193
    Thanks
    3
    Thanked 1 Time in 1 Post
    Is there a way using vba to move a file to the Windows Trashcan rather than just deleting it via a KILL statement?

  2. #2
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts
    [quote name='mcowen' post='798703' date='19-Oct-2009 00:36']Is there a way using vba to move a file to the Windows Trashcan rather than just deleting it via a KILL statement?[/quote]

    Good question...

    This seems to vary between versions of OS

    The location of the recycle bin varies between XP and Vista
    In Vista it is down to the user, so IF you are trying this on a network you could have issues.

    IN XP I think the Recycle Bin is called RECYCLER

    In Vista it is $Recycle.Bin\Key String

    The Key is the tough one to find because I think it is user specific

    Once you know it you can use File System Object to Move a File to the BIN

    Below is an example script that Does it for a named file

    You would need to modify it and check it.

    The example below is a Vista example'
    The XP might be simpler something like strTo="C:\RECYCLER\"

    Be careful!!! This is just a rough idea

    Function ToBin()

    Dim ofs As FileSystemObject
    Dim off As File
    Dim strPath As String, strTo As String, strFile As String

    strPath = "C:\DATA\"
    'This is a Vista Example
    strTo = "C:\$RECYCLE.BIN\S-1-5-21-2592367336-3883521446-1184989131-1000\"
    strFile = "Test.xlsx"

    Set ofs = New FileSystemObject
    Set off = ofs.GetFile(strPath & strFile)
    off.Move strTo

    DoEvents
    Set off = Nothing
    Set ofs = Nothing

    MsgBox "File " & strPath & strFile & " Deleted!"


    End Function


    Hopefully someone will come up with a much better and simpler solution.

    You will need to set a reference to Microsoft Scripting Runtime in the Tools References section.
    Andrew

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    See VBA Tips & Tricks: VBA Send File to Recycle Bin for code that uses Windows API functions; it doesn't require you to know what Andrew referred to as the "key string".

  4. #4
    2 Star Lounger
    Join Date
    Jul 2002
    Location
    Sacramento, CA
    Posts
    193
    Thanks
    3
    Thanked 1 Time in 1 Post
    Andrew & Hans,

    Thanks for the input, I went with the Tips & tricks version as it didn't require much additional thinking.
    Marty

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •