Restore given file from Recycle Bin

Post your working scripts, libraries and tools for AHK v1.1 and older
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Restore given file from Recycle Bin

13 Oct 2023, 06:18

Hello, people !

I wrote a sexy restore from recycle bin function. One just has to provide it with the original file path.

Code: Select all

restoreFileFromRecycleBin(userFileFullPath, userFileSize:=0) {
/*
userFileFullPath = the full path for the original file to restore
userFileSize     = match the file to restore by size (in bytes) as well
                   in recycle bin, the same original full path can occur
                   multiple times; today I delete a file which is 2mb, 
                   tomorrow one that is 5mb, but both had the same path;
                   which one to restore then? if you know the exact size 
                   in bytes, you can restore the file you actually want to

Return values:
 0 = file not found in recycle bin
 1 = file found in recycle bin and restored to userFileFullPath
-1 = file found in recycle bin but failed to restore
-2 = COM object error occured

; function by Marius Șucan
*/

   shellObj := ComObjCreate("Shell.Application")
   If !IsObject(shellObj)
      Return -2

   recyclerObj := shellObj.NameSpace(10) ; CSIDL_BitBucket // user recycle bin folder
   If !IsObject(recyclerObj)
   {
      ObjRelease(shellObj)
      Return -2
   }

   folderItemsObj := recyclerObj.Items()
   If !IsObject(folderItemsObj)
   {
      ObjRelease(recyclerObj)
      ObjRelease(shellObj)
      Return -2
   }

   foundFile := 0
   Loop, % folderItemsObj.Count + 1
   {
      fItemObj := folderItemsObj.Item(A_Index - 1)
      If (fItemObj.Size=0 || fItemObj.isLink!=0)
      {
         ObjRelease(fItemObj)
         Continue
      }

      ftype := SubStr(fItemObj.Path, InStr(fItemObj.Path, ".", 0, -1))
      fileName := recyclerObj.GetDetailsOf(fItemObj, 0)
      If !RegExMatch(fileName, "i)(\" ftype ")$")
         fileName .= ftype

      fullPath := recyclerObj.GetDetailsOf(fItemObj, 1) "\" fileName
      If (fullPath=userFileFullPath)
      {
         If (userFileSize>0)
         {
            z := fitemObj.Path
            FileGetSize, size, % z
            If (size=userFileSize)
               foundFile := 1
         } Else foundFile := 1

         If (foundFile=1)
         {
            ; fnOutputDebug(A_Index - 1 "==" date "|" size "|" fullPath)
            fItemVerbsObj := fItemObj.Verbs()
            TheItemVerbObj := fItemVerbsObj.Item(0)
            Sleep, 1
            TheItemVerbObj.DoIt()
            startTime := A_TickCount
            While, !FileExist(userFileFullPath)
            {
                If (A_TickCount - startTime>3500)
                   Break
                Sleep, 100
            }
            ObjRelease(TheItemVerbObj)
            ObjRelease(fItemVerbsObj)
            ObjRelease(fItemObj)
            Break
         }
      }
      ObjRelease(fItemObj)
   }

   ObjRelease(folderItemsObj)
   ObjRelease(recyclerObj)
   ObjRelease(shellObj)
   If (foundFile=1 && !FileExist(userFileFullPath))
      Return -1

   Return foundFile
}
Best regards, Marius.
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 93 guests