FileRecycle and NukeOnDelete

Propose new features and changes
iPhilip
Posts: 823
Joined: 02 Oct 2013, 12:21

FileRecycle and NukeOnDelete

25 Oct 2019, 13:53

I recently found out that when a Recycle Bin Location is marked "Don't move files to the Recycle Bin. Remove files immediately when deleted." (See Recycle Bin Properties) the FileRecycle command permanently deletes the file with a return value of 0 for ErrorLevel (no problem). I find that unsettling.

It turns out that, even though the code for FileRecycle includes it, the FOF_WANTNUKEWARNING flag
SHFILEOPSTRUCTW structure wrote: FOF_WANTNUKEWARNING
Send a warning if a file is being permanently destroyed during a delete operation rather than recycled. This flag partially overrides FOF_NOCONFIRMATION.
is not doing its job properly.

I would like to recommend that the FileRecycle command include a check of the NukeOnDelete registry value for the drive associated with the file. If that value is true, FileRecycle should do nothing and set ErrorLevel to 1. Below is a function that performs the check:

Code: Select all

IsNukeOnDelete(FilePattern) {
   static TCHAR_Size := A_IsUnicode ? 2 : 1
   
   ; Get the drive letter associated with FilePattern
   Size := DllCall("Kernel32.dll\GetFullPathName", "Str", FilePattern, "UInt", 0, "Ptr", 0, "PtrP", 0, "UInt")
   VarSetCapacity(FullPathName, Size * TCHAR_Size, 0)
   DllCall("Kernel32.dll\GetFullPathName", "Str", FilePattern, "UInt", Size, "Str", FullPathName, "PtrP", 0, "UInt")
   SplitPath, FullPathName, , , , , Drive
   
   ; Get the GUID associated with the drive
   VarSetCapacity(VolumeName, 50 * TCHAR_Size, 0)
   DllCall("Kernel32.dll\GetVolumeNameForVolumeMountPoint", "Str", Drive "\", "Str", VolumeName, "UInt", 50, "Int")
   RegExMatch(VolumeName, "\{.*}", GUID)
   
   ; Now that we have the GUID, we can look up the value of NukeOnDelete in the registry
   RegRead, NukeOnDelete, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BitBucket\Volume\%GUID%, NukeOnDelete
   Return NukeOnDelete
}
Until such a check is implemented, a safe way to use FileRecycle might be as follows:

Code: Select all

if !IsNukeOnDelete(FilePattern)
   FileRecycle, %FilePattern%
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
lexikos
Posts: 9621
Joined: 30 Sep 2013, 04:07
Contact:

Re: FileRecycle and NukeOnDelete

25 Oct 2019, 20:55

This function may permanently delete the file if it is too large to be recycled; as of [v1.0.96], a warning should be shown before this occurs.
That's what FOF_WANTNUKEWARNING does.

The Recycle Bin is (usually?) also disabled on removable drives, and in that case FileRecycle deletes the file.

In my opinion, if the user or administrator has disabled the Recycle Bin, the file should be deleted.

I do not think it is sensible to rely on the Recycle Bin as a holding place for items you may want to keep. "Recycling" a file is like throwing it into the household rubbish bin. If you throw something out by accident and realize before the bin is emptied, you can recover it; but I hope you don't throw items in the bin with the intention of taking them out again later. For large items, you might need to take them straight to the dump (large files are nuked), and recovery is unlikely. If you're out of the house (removable drive), you (hopefully) use a public bin, and recovery is unlikely.

However, the current documentation is not clear about what happens if recycling the file is not possible, and that should be corrected.
iPhilip
Posts: 823
Joined: 02 Oct 2013, 12:21

Re: FileRecycle and NukeOnDelete

20 Jan 2020, 14:43

Hi @lexikos,
lexikos wrote:
25 Oct 2019, 20:55
This function may permanently delete the file if it is too large to be recycled; as of [v1.0.96], a warning should be shown before this occurs.
That's what FOF_WANTNUKEWARNING does.
Thank you for the clarification.
lexikos wrote:
25 Oct 2019, 20:55
The Recycle Bin is (usually?) also disabled on removable drives, and in that case FileRecycle deletes the file.

In my opinion, if the user or administrator has disabled the Recycle Bin, the file should be deleted.

I do not think it is sensible to rely on the Recycle Bin as a holding place for items you may want to keep. "Recycling" a file is like throwing it into the household rubbish bin. If you throw something out by accident and realize before the bin is emptied, you can recover it; but I hope you don't throw items in the bin with the intention of taking them out again later. For large items, you might need to take them straight to the dump (large files are nuked), and recovery is unlikely. If you're out of the house (removable drive), you (hopefully) use a public bin, and recovery is unlikely.
I like the analogy of the rubbish bin. :) I would extend it by likening the case of marking "Don't move files to the Recycle Bin. Remove files immediately when deleted." to installing an automatic trash compactor into the rubbish bin without putting a warning label on it reminding you that the item won't be recoverable, at least in its original form :lol:.
lexikos wrote:
25 Oct 2019, 20:55
However, the current documentation is not clear about what happens if recycling the file is not possible, and that should be corrected.
I saw that you added a note in the Remarks section of the FileRecycle documentation page. Thank you.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Return to “Wish List”

Who is online

Users browsing this forum: Google [Bot] and 11 guests