Jump to content

Recycle Bin and FileRecycleEmpty


 Share

Recommended Posts

Hi everyone,

I have a simple script that empties the recycle bin, using FileRecycleEmpty(). The script pops up a message after FileRecycleEmpty runs, with a message based on the return value of FileRecycleEmpty. If FileRecycleEmpty = 1, the message pops up that the recycle bin was emptied successfully. If FileRecycleEmpty = 0, the message states that the empty operation failed. What I have noticed is that if the recycle bin is empty, the return value for FileRecycleEmpty is zero. What I want to do is to make the failure message more specific. At the moment, the message says "Unable to empty Recycle Bin or Recycle Bin was already empty". What I would like is two messages. One that says "Unable to empty Recycle Bin" (for example, there is a problem with the file permissions or file attributes) and the other that says "Recycle Bin was already empty" (the reason for the two separate messages is that the former case requires fixing, the latter case does not).

FileRecycleEmpty does not have a way to distinguish between these two cases (an already empty recycle bin and a genuine problem with the recycle bin) that I can see, and I have been unable to find anything with the forum search relating to a similar problem or how to solve it. I did think of trying to count the number of files in the recycle bin before the FileRecycleEmpty command (an empty recycle bin has a single folder named after the SID of your Windows user account with a single desktop.ini file inside it - so if the number of folders was 1 and the number of files was 1, the bin should already be empty), but I have been unable to find anything with the forum search on how exactly to count files and folders with AutoIt. Does anyone have any suggestions as to how I could solve my recycle problem?

Thanks.

Link to comment
Share on other sites

Before trying to empty the recycle bin check if it contains anything with _RecycleBin_Check(0)...

MsgBox(0, "", _RecycleBin_Check(0))
MsgBox(0, "", _RecycleBin_Check())

Func _RecycleBin_Check($iReturnType = 1)
    Local $SHQUERYRBINFO
    If @AutoItX64 Then
        $SHQUERYRBINFO = DllStructCreate("DWORD cbSize;int64 i64Size;int64 i64NumItems")
    Else
        $SHQUERYRBINFO = DllStructCreate("align 1;int;int64;int64")
    EndIf
    DllStructSetData($SHQUERYRBINFO, 1, DllStructGetSize($SHQUERYRBINFO))

    Local $iRecycleBin_Items, $sReturn

    $aRecycleBin_Drives = DriveGetDrive("Fixed")
    If Not @error Then
        For $i = 1 To $aRecycleBin_Drives[0]
            If DriveStatus($aRecycleBin_Drives[$i] & "\") = "READY" Then
                $Query = DllCall("shell32.dll", "int", "SHQueryRecycleBin", "str", $aRecycleBin_Drives[$i] & "\", "ptr", DllStructGetPtr($SHQUERYRBINFO))
                $iRecycleBin_Items += DllStructGetData($SHQUERYRBINFO, 3)
                Switch $iReturnType
                    Case 1
                        $sReturn &= $aRecycleBin_Drives[$i] & "\ " & @TAB & DllStructGetData($SHQUERYRBINFO, 3) & " items, " & Ceiling(DllStructGetData($SHQUERYRBINFO, 2) / 1024) & " KBytes" & @CRLF
                    Case Else
                        $sReturn += DllStructGetData($SHQUERYRBINFO, 2)
                EndSwitch
            EndIf
        Next
    EndIf
    Return $sReturn

EndFunc   ;==>_RecycleBin_Check

Maybe a question for the real pros :x...

If @AutoItX64 Then
        $SHQUERYRBINFO = DllStructCreate("DWORD cbSize;int64 i64Size;int64 i64NumItems")
    Else
        $SHQUERYRBINFO = DllStructCreate("align 1;int;int64;int64")
    EndIf

The first structure (@AutoItX64) is as defined at MSDN, but it does not return the correct value on x64 when run from a 32bit process, only works with the workaround above.

Edited by KaFu
Link to comment
Share on other sites

Hi,

Thanks for that. I will try to integrate that code into my current script and see if I can get it working. For reference, this is the current code:

AutoIt Version: 3.3.6.1

 Script Function: Empties Recycle Bin (if I have many windows open, running this .exe rather than minimising all windows, right clicking on the Bin and emptying it that way saves a lot of time).

 Notes:
 1. Note that if you use an "Attribute" of "1" in SplashTextOn, you should remove the "- 75" from $Position_Height and the "- 10" from $Position_Width, as the resulting splash screen will no longer have borders of approximately 5 pixels in size and will therefore not need the adjustment.
 2. The intention is that the splash screen does not show until the FileRecycleEmpty() has fully completed (i.e. all the files have been deleted from the Recycle Bin). I have quickly tested this with a couple of GB's of files in the Recycle Bin, and it did indeed seem that the Recycle Bin icon changed to 'empty' before the splash screen displayed. So I think that this is the case. The AutoIt helpfile also says that the function returns a code to indicate success or failure - I believe that would mean that SplashTextOn would not execute until that code was returned (i.e. the files had been deleted). Currently, the AutoIt tray icon is still showing while the splash screen is displaying. I am not sure if this means that the FileRecycleEmpty() is still running, or if indeed that has finished and the tray icon is only showing because the script has not exited (as it is still busy showing the splash screen). So, to summarise: I am confident the splash screen is shown after all the files have been deleted, but am not 100% sure.
 3. There are two sets of variables: fail and success. This is to account for the bigger size of the splash screen (due to a bigger font being used) if the empty recycle bin operation was not successful. Admittedly, you could use a MsgBox for the fail if you wanted. That would return the number of variables to one set (success only).

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

; Failed Recycle Bin empty variables.
$Splash_Height_Fail = 75 ; Height of the failed "Recycle Bin Emptied" Splash Screen.
$Splash_Width_Fail = 350 ; Width of the failed "Recycle Bin Emptied" Splash Screen.
$Position_Height_Fail = @DesktopHeight - $Splash_Height_Fail - 75 ; Position on screen (height wise, i.e. the x-axis) of the failed "Recycle Bin Emptied" splash screen. Takes the current desktop resolution, subtracts the height of the splash screen and then subtracts another 75 pixels (approximate height of the Windows taskbar plus the border of the splash screen itself, which are about 10 pixels in total - two borders top and bottom of approximately 5 pixels each). Thus, the final position of the splash screen (height wise) should be just above the taskbar.
$Position_Width_Fail = @DesktopWidth - $Splash_Width_Fail - 10 ; Position on screen (width wise, i.e. the y-axis) of the failed "Recycle Bin Emptied" splash screen. Takes the current desktop resolution, subtracts the width of the splash screen and then subtracts another 10 pixels (the borders of the splash screen itself in total are about 10 pixels - two borders left and right of approximately 5 pixels each). Thus, the final position of the splash screen (width wise) should be just touching the right hand side of the screen.

; Successful Recycle Bin empty variables.
$Splash_Height_Success = 50 ; Height of the successful "Recycle Bin Emptied" Splash Screen.
$Splash_Width_Success = 250 ; Width of the successful "Recycle Bin Emptied" Splash Screen.
$Position_Height_Success = @DesktopHeight - $Splash_Height_Success - 75 ; Position on screen (height wise, i.e. the x-axis) of the successful "Recycle Bin Emptied" splash screen. Takes the current desktop resolution, subtracts the height of the splash screen and then subtracts another 75 pixels (approximate height of the Windows taskbar plus the border of the splash screen itself, which are about 10 pixels in total - two borders top and bottom of approximately 5 pixels each). Thus, the final position of the splash screen (height wise) should be just above the taskbar.
$Position_Width_Success = @DesktopWidth - $Splash_Width_Success - 10 ; Position on screen (width wise, i.e. the y-axis) of the successful "Recycle Bin Emptied" splash screen. Takes the current desktop resolution, subtracts the width of the splash screen and then subtracts another 10 pixels (the borders of the splash screen itself in total are about 10 pixels - two borders left and right of approximately 5 pixels each). Thus, the final position of the splash screen (width wise) should be just touching the right hand side of the screen.

$Status = FileRecycleEmpty ( ) ; Empty the recycle bin for all drives and assign exit code to Status variable.

If $Status = 0 Then ; If FileRecycleEmpty() was unsuccessful in emptying the Recycle Bin (file permissions or some file attributes prevent files from being deleted for example).
    SplashTextOn ( "Error!", "Unable to empty Recycle Bin or"&@LF&"Recycle Bin was already empty.", $Splash_Width_Fail, $Splash_Height_Fail, $Position_Width_Fail, $Position_Height_Fail, "", -1, 15 ) ; Create the splash screen.
    Sleep ( 3000 ) ; Sleep for 3 seconds (i.e. splash screen will display for this long). It's an error dialog, so sleep is longer than for the success dialog.
    SplashOff ( ) ; Turn off the splash screen.
Else ; If FileRecycleEmpty() was successful in emptying the Recycle Bin.
    SplashTextOn ( "Recycle Bin Emptied!", "Recycle bin empty complete.", $Splash_Width_Success, $Splash_Height_Success, $Position_Width_Success, $Position_Height_Success ) ; Create the splash screen.
    Sleep ( 1500 ) ; Sleep for 1.5 seconds (i.e. splash screen will display for this long).
    SplashOff ( ) ; Turn off the splash screen.
EndIf

Exit ; Exit the script.

Regards.

Edited by romulous
Link to comment
Share on other sites

KaFu I am no where near a Professional but I just checked _WinAPI_ShellQueryRecycleBin() in WinAPIEx.au3 and it appears Yashied only uses $SHQUERYRBINFO = DllStructCreate("align 1;int;int64;int64") for x32 & x64! Nice Function though :x

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Oh, got this function in my snippet folder a little longer and didn's see that Yashied already added it :x... though testing it, it returns the same false values (better an @error) if run in a 64bit process... I'll report it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...