PDA

View Full Version : Determine if folder is Recycle Bin



sglo
06-07-2011, 11:59 AM
Hello,
We have an Excel based program that attempts to locate old program templates during an update. We'd like to ignore any old templates that reside only in the Recycle Bin. Unfortunately, I understand that the recycle bin location varies by operating system and although our users are all using some form of Windows, that's all I can guarantee. The code below that checks folder names to rule out the recycle bin makes me nervous since I fear it may not be sufficiently robust to catch all versions. Expert opinion?
'*****************************************************
Function isRecycleBin(folderName As String) As Boolean
'@param folderName the current folder
'@return true if "Recycle" is contained in folderName, false otherwise
Dim pos As Integer, isRecycle As Boolean
pos = InStr(LCase(folderName), "recycle")
'If pos is greater than 0 then assume the folder is the recycle bin
If (pos > 0) Then
isRecycle = True
Else
isRecycle = False
End If
isRecycleBin = isRecycle
End Function

Kenneth Hobs
06-07-2011, 12:59 PM
Welcome to the forum!

There is no folder path to it so I don't know how that would work. We can use methods to recycle files rather than delete them or delete them from the recycle bin. It is not the same as working with folders.

sglo
06-07-2011, 01:10 PM
I can vouch that this does in fact catch files in the recycle bin for at least some versions of windows 7. The problem is that we want to replace templates, but not if they are already in the recycle bin so we need to be able to tell if they are in a normal location and can be deleted or are in a recycle bin and we can just ignore them. Any thoughts?

Kenneth Hobs
06-07-2011, 01:15 PM
I don't have win7 so I can not test that scenario. If you post an example as to how it got the path, we can see if it would catch the path in xp or vista. Maybe you have an environment variable or WOW API that has the path?

Of course your routine only shows if the foldername contains the word "recycle". This does not mean that it would tell you if it were the Recycle Bin.

sglo
06-07-2011, 03:54 PM
Yes, that is a known flaw but "recycle" is a directory name our users aren't likely to have the files in unless it's related to the recycle bin and it's better for us to err on the side of skipping over files that should be deleted than to try to delete files for which we don't have permission.
One search returned the following paths:
C:\RECYCLER\S-1-5-21-3268904920-224824091-1924704943-1005\DcB\Template1.xlsm
C:\RECYCLER\S-1-5-21-3268904920-224824091-1924704943-1005\DcB\Template2.xlsx
C:\RECYCLER\S-1-5-21-3268904920-224824091-1924704943-1005\DcB\Template3.xlsx.
I had to type these in manually because I'm working from a screenshot returned by one of the users who had trouble because the code was trying to delete these templates but didn't have permission b/c they were in the Recycle Bin. What we'd like the code to do is recognize that these paths are paths leading to the recycle bin and just ignore them, go on and look for others. For this user, searching for "RECYCLER" in the path appears to be the way to go but I'm having a hard time finding documentation about how to check in general for different windows operating systems. All I really need is to be able to tell without opening a file if it has been recycled- if there is a better way to do so I'm all over it.
Thanks for your help!


I don't have win7 so I can not test that scenario. If you post an example as to how it got the path, we can see if it would catch the path in xp or vista. Maybe you have an environment variable or WOW API that has the path?

Of course your routine only shows if the foldername contains the word "recycle". This does not mean that it would tell you if it were the Recycle Bin.

Paul_Hossler
06-07-2011, 05:10 PM
http://stackoverflow.com/questions/1585295/how-can-i-tell-that-a-directory-is-the-recycle-bin-in-c



There's a little problem here. The Windows Recycle Bin is a virtual folder and does not actually exist. The files that you see are not actually in that folder, they are the representation of existing files on disk that have been renamed to a special name, which "removes" them from the visible file system, but not the physical one.
You can "proof" this for yourself by asking for the folder location using the win32 API. It will return E_FAIL for the Recycle Bin, but not for other folders (see SHGetKnownFolderPath on pinvoke.net (http://www.pinvoke.net/default.aspx/shell32/SHGetKnownFolderPath.html) (and on MSDN (http://msdn.microsoft.com/en-us/library/bb762188%28VS.85%29.aspx)) for all constants you can use and the declarations needed for this code to run):




But all is not lost. This fake non-existing "file name" or "folder name" contains a hidden file that looks something like "S-1-5-21-2703390745-3900912742-210389625-1000" (yours will be different). It's one of two "reliable" ways to find out whether a certain filename is actually a virtual directory of the recycle bin (the other way being: delete a file through SHFileOperation (http://msdn.microsoft.com/en-us/library/bb762164%28VS.85%29.aspx), explained here (http://www.codeproject.com/KB/shell/recyclebin.aspx), and check whether it appears in the folder you have):


The second quote suggests testing to see if the folder contains "S-1-5-21" or to programtically 'recycle' a dummy file and see if it shows up in the folder.

I have a VBA module to send a file to the recycle bin if you want

Paul

sglo
06-07-2011, 06:24 PM
Thank you. Unfortunately, I don't want to send a file to the recycle bin, I want to eliminate false positives from a list of files returned by a search procedure looking for our templates. Currently the search procedure is picking up files in recycle bins for some users and this causes problems. I want it to ignore files that have already been recycled.

So all I want to know is if a file has been recycled. If so, I want to ignore it. I do not need to move files to the recycler, only identify which files are already there.

[quote=Paul_Hossler

The second quote suggests testing to see if the folder contains "S-1-5-21" or to programtically 'recycle' a dummy file and see if it shows up in the folder.

I have a VBA module to send a file to the recycle bin if you want

Paul[/quote]

sglo
06-07-2011, 06:30 PM
I do note that the first quote gives a theoretical way to distinguish between Recycle Bin virtual folders and others but in order for our macros to pass some security checks we are under directions not to use API calls. I'm not an expert so perhaps this would not be a violation but I suspect I need to find another workaround.
I'm checking into the links to see if there's anything else I think I can use but I'm not seeing it yet...







The second quote suggests testing to see if the folder contains "S-1-5-21" or to programtically 'recycle' a dummy file and see if it shows up in the folder.

I have a VBA module to send a file to the recycle bin if you want

Paul

Paul_Hossler
06-07-2011, 06:44 PM
Actually I was thinking that you could test the full path of the file that you found and see if it contained "S-1-5-21"

There's no API call required for that

so if the filepath you wanted to test (e.g. C:\$Recycle Bin\S-1-5-21-2595827324-169963834-2702095184-1001\$IFE4PLM.xlsm)

then InStr(filepath, "S-1-5-21") > 0 would tell you that it was in the Bin

Paul

sglo
06-07-2011, 08:04 PM
I could definitely do that but I'm concerned about how robust it would be since I don't know how standard those numbers are going to be from system to system. The phrasing "yours will be different" in that post gives me uneasy feelings. I need to adapt the code so it will work for all the users across their various operating systems.

I'm thinking that I may have to create a temporary file, put it in the recycle bin, search for it to get the new location, and use the first part of that new location as my recycle bin identifier. I really don't like that solution since I don't know ahead of time where the user will have write privileges and the current code is set up to delete the old templates before querying about where to put the new ones. So it'll be an inelegant solution that requires significant re-organization of the existing code. Grrr. This isn't code I've had much to do with before so I keep hoping there's a better solution I just haven't found yet.



Actually I was thinking that you could test the full path of the file that you found and see if it contained "S-1-5-21"

There's no API call required for that

so if the filepath you wanted to test (e.g. C:\$Recycle Bin\S-1-5-21-2595827324-169963834-2702095184-1001\$IFE4PLM.xlsm)

then InStr(filepath, "S-1-5-21") > 0 would tell you that it was in the Bin

Paul

santosh59211
01-29-2012, 06:32 AM
same problem is also mine.. hope u ppl help us

sglo
01-30-2012, 09:18 AM
I never did find a *good* solution. If you find something I hope you'll post a link back here. The solution we wound up with basically checks for the different variations on "RECYCLE" in the file path. It has the potential to generate both false positives and false negatives but we couldn't justify putting any more time into it.

same problem is also mine.. hope u ppl help us