{"id":20843,"date":"2008-09-18T10:00:00","date_gmt":"2008-09-18T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/09\/18\/how-can-i-tell-that-a-directory-is-really-a-recycle-bin\/"},"modified":"2008-09-18T10:00:00","modified_gmt":"2008-09-18T10:00:00","slug":"how-can-i-tell-that-a-directory-is-really-a-recycle-bin","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080918-00\/?p=20843","title":{"rendered":"How can I tell that a directory is really a recycle bin?"},"content":{"rendered":"<p><P>\nHere&#8217;s a question inspired by an actual customer question:\n<\/P>\n<BLOCKQUOTE CLASS=\"q\">\nI need a function that, given a path, tells me whether it is\na Recycle Bin folder.\nI tried using functions like <CODE>SHGetSpecialFolderPath<\/CODE>\nwith <CODE>CSIDL_BITBUCKET<\/CODE>, but that doesn&#8217;t work because\nthe Recycle Bin is a virtual folder that is the union of the\nRecycle Bins of all drives.\n<\/BLOCKQUOTE>\n<P>\nThe customer noted that they don&#8217;t want to hard-code the words\n<CODE>RECYCLED<\/CODE> and <CODE>RECYCLER<\/CODE>,\nwhich is a good decision because\nthe name of the directory\ndepends on many things.\nI mentioned earlier that\n<A HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/01\/31\/520225.aspx\">\nit depends on the file system<\/A>.\nIt also depends on whether the drive is accessed locally or remotely;\nnetwork-based Recycle Bin folders follow yet another naming scheme.\nIt may even depend on what operating system the user is running.\nNo, hard-coding the name of the Recycle Bin folders is not a good idea.\n<\/P>\n<P>\nThe <CODE>SHDESCRIPTIONID<\/CODE> structure tells you a little more\nabout a shell folder.\nIn addition to the &#8220;description ID&#8221;, it also gives you a <CODE>CLSID<\/CODE>,\nand it is the <CODE>CLSID<\/CODE> that is relevant here.\n<\/P>\n<PRE>\n#include &lt;windows.h&gt;\n#include &lt;shlobj.h&gt;\n#include &lt;tchar.h&gt;\n#include &lt;stdio.h&gt;<\/p>\n<p>HRESULT\nGetFolderDescriptionId(LPCWSTR pszPath, SHDESCRIPTIONID *pdid)\n{\n  HRESULT hr;\n  LPITEMIDLIST pidl;\n  if (SUCCEEDED(hr = SHParseDisplayName(pszPath, NULL,\n                                        &amp;pidl, 0, NULL))) {\n    IShellFolder *psf;\n    LPCITEMIDLIST pidlChild;\n    if (SUCCEEDED(hr = SHBindToParent(pidl, IID_IShellFolder,\n                                    (void**)&amp;psf, &amp;pidlChild))) {\n      hr = SHGetDataFromIDList(psf, pidlChild,\n                     SHGDFIL_DESCRIPTIONID, pdid, sizeof(*pdid));\n      psf-&gt;Release();\n    }\n    CoTaskMemFree(pidl);\n  }\n  return hr;\n}<\/p>\n<p>int __cdecl wmain(int argc, WCHAR **argv)\n{\n  SHDESCRIPTIONID did;\n  if (SUCCEEDED(GetFolderDescriptionId(argv[1], &amp;did)) &amp;&amp;\n      did.clsid == CLSID_RecycleBin) {\n    printf(&#8220;is a recycle bin\\n&#8221;);\n  } else {\n    printf(&#8220;is not a recycle bin\\n&#8221;);\n  }\n  return 0;\n}\n<\/PRE>\n<P>\nThe <CODE>GetFolderDescriptionId<\/CODE> function\ntakes the path to a folder\nand converts it to an <CODE>ITEMIDLIST<\/CODE>\njust so it can call <CODE>SHGetDataFromIDList<\/CODE>\nto obtain the <CODE>SHDESCRIPTIONID<\/CODE>.\nAll we care about in this case\nis whether the <CODE>CLSID<\/CODE>\nis the Recycle Bin or not.\n<\/P>\n<PRE>\nC:\\&gt; checkrecycle C:\\Windows\nis not a recycle bin\nC:\\&gt; checkrecycle C:\\RECYCLER\\S-1-5-21-2127521184-1604012920-1887927527-72713\nis a recycle bin\n<\/PRE>\n<P>\nOf course, now that I told you how to do it, I have to tell\nyou that this is not how to do it.\nThis is another example of a customer having a problem,\nsolving half of it, and\n<A HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/03\/23\/558887.aspx\">\nasking for help with the other half<\/A>,\nunaware that they are approaching the problem from the wrong\ndirection.\nNext time, we&#8217;ll look at the customer&#8217;s <I>real<\/I> problem.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a question inspired by an actual customer question: I need a function that, given a path, tells me whether it is a Recycle Bin folder. I tried using functions like SHGetSpecialFolderPath with CSIDL_BITBUCKET, but that doesn&#8217;t work because the Recycle Bin is a virtual folder that is the union of the Recycle Bins of [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-20843","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Here&#8217;s a question inspired by an actual customer question: I need a function that, given a path, tells me whether it is a Recycle Bin folder. I tried using functions like SHGetSpecialFolderPath with CSIDL_BITBUCKET, but that doesn&#8217;t work because the Recycle Bin is a virtual folder that is the union of the Recycle Bins of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/20843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=20843"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/20843\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=20843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=20843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=20843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}