In this post, I tried to enumerate files within recycle bin using a small Win32 program. As we know that recycle bin is a special folder on Windows File System. The location of this directory is not in the registry; it is marked with hidden and system attributes to prevent the user from moving or
deleting it.
The steps to list out the contents of Recycle bin are as below:
1. It's a special folder denoted by CSIDL_BITBUCKET, which we need to pass to the function SHGetFolderLocation() method.
2. Source code snippet, I've avoided checks as much as possible to make the code simple and clean:
int _tmain(int argc, _TCHAR* argv[])
{
LPITEMIDLIST pidlWinRecycleFiles = NULL;
LPITEMIDLIST pidlItems = NULL;
IShellFolder *psfWinRecycleFiles = NULL;
IShellFolder *psfDeskTop = NULL;
LPENUMIDLIST ppenum = NULL;
STRRET strDispName;
TCHAR pszParseName[MAX_PATH];
ULONG celtFetched;
HRESULT hr;
hr = SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, NULL, &pidlWinRecycleFiles);
hr = SHGetDesktopFolder(&psfDeskTop);
hr = psfDeskTop->BindToObject(pidlWinRecycleFiles, NULL, IID_IShellFolder, (LPVOID *) &psfWinRecycleFiles);
hr = psfDeskTop->Release();
hr = psfWinRecycleFiles->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1)
{
// Name of file and folder in recycle bin
psfWinRecycleFiles->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName);
StrRetToBuf(&strDispName, pidlItems, pszParseName, MAX_PATH);
// Path of file
psfWinRecycleFiles->GetDisplayNameOf(pidlItems, SHGDN_NORMAL, &strDispName);
StrRetToBuf(&strDispName, pidlItems, pszParseName, MAX_PATH);
PathRemoveFileSpec(pszParseName);
CoTaskMemFree(pidlItems);
}
ppenum->Release();
CoTaskMemFree(pidlWinRecycleFiles);
psfWinRecycleFiles->Release();
return 0;
}
We need to include the library 'Shlwapi.lib'.
The steps to list out the contents of Recycle bin are as below:
1. It's a special folder denoted by CSIDL_BITBUCKET, which we need to pass to the function SHGetFolderLocation() method.
2. Source code snippet, I've avoided checks as much as possible to make the code simple and clean:
int _tmain(int argc, _TCHAR* argv[])
{
LPITEMIDLIST pidlWinRecycleFiles = NULL;
LPITEMIDLIST pidlItems = NULL;
IShellFolder *psfWinRecycleFiles = NULL;
IShellFolder *psfDeskTop = NULL;
LPENUMIDLIST ppenum = NULL;
STRRET strDispName;
TCHAR pszParseName[MAX_PATH];
ULONG celtFetched;
HRESULT hr;
hr = SHGetFolderLocation(NULL, CSIDL_BITBUCKET, NULL, NULL, &pidlWinRecycleFiles);
hr = SHGetDesktopFolder(&psfDeskTop);
hr = psfDeskTop->BindToObject(pidlWinRecycleFiles, NULL, IID_IShellFolder, (LPVOID *) &psfWinRecycleFiles);
hr = psfDeskTop->Release();
hr = psfWinRecycleFiles->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1)
{
// Name of file and folder in recycle bin
psfWinRecycleFiles->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName);
StrRetToBuf(&strDispName, pidlItems, pszParseName, MAX_PATH);
// Path of file
psfWinRecycleFiles->GetDisplayNameOf(pidlItems, SHGDN_NORMAL, &strDispName);
StrRetToBuf(&strDispName, pidlItems, pszParseName, MAX_PATH);
PathRemoveFileSpec(pszParseName);
CoTaskMemFree(pidlItems);
}
ppenum->Release();
CoTaskMemFree(pidlWinRecycleFiles);
psfWinRecycleFiles->Release();
return 0;
}
We need to include the library 'Shlwapi.lib'.
Comments