{"id":66943,"date":"2006-07-10T08:06:00","date_gmt":"2006-07-10T08:06:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/07\/10\/how-can-i-list-all-the-empty-folders-on-a-specified-drive\/"},"modified":"2006-07-10T08:06:00","modified_gmt":"2006-07-10T08:06:00","slug":"how-can-i-list-all-the-empty-folders-on-a-specified-drive","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-all-the-empty-folders-on-a-specified-drive\/","title":{"rendered":"How Can I List All the Empty Folders on a Specified Drive?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I list all the empty folders (that is, folders with a size of 0 bytes) on a specified drive?<BR><BR>&#8212; DD<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DD. You know, physicist Edwin Schrodinger once proposed a thought experiment in which a cat is placed in a sealed box, along with a canister of poison gas. The experiment is designed in such a way that there is exactly a 50-50 chance that the canister will release the gas, killing the cat instantly. According to quantum mechanics, until you open the box and check for yourself, the cat is somehow both dead <I>and<\/I> alive. One of the great paradoxes of modern life.<\/P>\n<TABLE id=\"E3C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. So why is this just a thought experiment and not a <I>real<\/I> experiment? We don\u2019t know for sure, but imagine this: you place a cat in a sealed box, along with a canister of poison gas. Now suppose that the canister doesn\u2019t release the gas. Would <I>you<\/I> want to be the poor researcher who had to open the box and deal with what would likely be one crazy-mad cat? Obviously Schrodinger had no desire to be that person, either. <I>(Editor\u2019s Note: There\u2019s also the small detail that you might actually kill a cat.)<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Another famous paradox is the Grandfather Paradox. Suppose you go back in time and murder your own grandfather. Obviously that would mean that you would never be born. But if you were never born how were you able to go back in time and kill your own grandfather? And if you <I>were<\/I> able to go back in time, why the heck did you go back and kill your grandfather instead of bringing back a bunch of Willy Wonka Scrumdidlyuptious Bars, quite possibly the best candy bar ever made? <\/P>\n<P>At the very least, go back and kill the grandfather of the person who decided to stop making the Scrumdidlyuptious Bar.<\/P>\n<P>So is any of this relevant to retrieving a list of all the empty folders on a specified drive? Kind of; after all, it turns out that getting a list of empty folders is both harder than you might expect and, at the same time, easier than you might expect. Call it the Empty Folders Paradox. (We were going to call it the Grandfather Paradox, but that name was already taken.)<\/P>\n<P>Let\u2019s start off by talking about why it\u2019s harder than you might expect. At first glance this sounds pretty easy; after all, you can use WMI\u2019s Win32_Directory class to return a collection of all the folders on a drive. Can\u2019t you just write a query that asks for all the folders that have a FileSize equal to 0?<\/P>\n<P>Sure, except that you\u2019ll be disappointed in the results. That\u2019s because, as far as WMI is concerned, <I>all<\/I> folders have the same FileSize: no size. Because the FileSize property isn\u2019t fully-implemented on the Win32_Directory class, FileSize always come back as Null.<\/P>\n<P>Always.<\/P>\n<P>That\u2019s a problem: not only does it eliminate your ability to use WMI to retrieve a list of empty folders, but it also makes it a bit tricky to perform this task against remote computers. And yet, paradoxically, although the FileSize problem makes this task harder than you\u2019d expect, the final solution (which uses the FileSystemObject) is easier than you might expect. In fact, that final solution requires no more code than this:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set FSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nShowSubFolders FSO.GetFolder(&#8220;C:\\&#8221;)<\/p>\n<p>Sub ShowSubFolders(Folder)\n    For Each Subfolder in Folder.SubFolders\n        If Subfolder.Size = 0 Then\n            Wscript.Echo Subfolder.Path\n        End If\n        ShowSubFolders Subfolder\n    Next\nEnd Sub\n<\/PRE>\n<P>As you can see, we start out by implementing the <B>On Error Resume Next<\/B> statement (just in case we run into any problems). We then create an instance of the <B>Scripting.FileSystemObject<\/B>. That brings us to this line of code:<\/P><PRE class=\"codeSample\">ShowSubFolders FSO.GetFolder(&#8220;C:\\&#8221;)\n<\/PRE>\n<P>ShowSubFolders is a \u201crecursive subroutine\u201d that we\u2019ll use &#8211; repeatedly &#8211; in order to get at all the subfolders in C:\\, including all the subfolders in those subfolders, and the subfolders in those sub-subfolders, etc. We won\u2019t discuss recursion in any detail today; that goes a bit beyond the scope of this column. (If you thought the Grandfather Paradox was difficult to conceptualize, wait till you try tackling recursion!) However, you can find a brief introduction to recursion in the <I>Microsoft Windows 2000 Scripting Guide<\/I>. <\/P>\n<TABLE id=\"ENE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. No, you don\u2019t have to go back in time to the year 2000 in order to read the Scripting Guide; just access it <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_vbs_kove.mspx\" target=\"_blank\"><B>online<\/B><\/A>. If you <I>do<\/I> go back in time, though, say hi to your grandfather for us.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Inside our subroutine we use this line of code to retrieve a collection of all the top-level subfolders found in C:\\:<\/P><PRE class=\"codeSample\">For Each Subfolder in Folder.SubFolders\n<\/PRE>\n<P>Good question: what <I>do<\/I> we mean by \u201ctop-level\u201d subfolders? That means we\u2019re going to get back a subfolder like C:\\Scripts; however, we will <I>not<\/I> get back any sub-subfolders like C:\\Scripts\\WMI or C:\\Scripts\\ADSI. The SubFolders property works on only one level of subfolders at a time. But we\u2019ll show you how to get around that issue in just a minute.<\/P>\n<P>Once we have a collection of subfolders we use this line of code to determine whether the folder is empty (that is, whether or not it has a <B>Size<\/B> equal to 0):<\/P><PRE class=\"codeSample\">If Subfolder.Size = 0 Then\n<\/PRE>\n<P>If the Size <I>is<\/I> equal to 0 we echo back the folder <B>Path<\/B>. If the Size is not equal to 0, well, in that case we don\u2019t do anything at all.<\/P>\n<P>This is where things get a little paradoxical:<\/P><PRE class=\"codeSample\">ShowSubFolders SubFolder\n<\/PRE>\n<P>You\u2019re right: we\u2019re already inside the ShowSubFolders subroutine and yet now we\u2019re calling that same very same subroutine. That sounds crazy (and it is), but such is the nature of recursion. The basic idea is this: we start out in C:\\ and get a collection of folders. Let\u2019s say the first folder we get back is C:\\Scripts. We check the file size, then call the ShowSubFolders subroutine to get back a new collection, one consisting of all the subfolders found in C:\\Scripts. Let\u2019s assume there\u2019s just one such subfolder: C:\\Scripts\\WMI. We check the file size and then call ShowSubFolders <I>again<\/I>, this time to see if C:\\Scripts\\WMI has any subfolders. This process continues until we run out of subfolders, in which case we go back to C:\\ and check the next subfolder in <I>that<\/I> collection.<\/P>\n<P>That\u2019s OK; it gives us a headache, too. But it works and, fortunately, you don\u2019t have to keep track of which subfolders you\u2019ve looked at and which subfolders you haven\u2019t looked at; VBScript takes care of that for you. <\/P>\n<TABLE id=\"EEG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Originally VBScript <I>didn\u2019t<\/I> keep track of that stuff for you. But we sent Peter back in time and had him change that. He was supposed to bring us some candy bars, too, but he said he got hungry on the trip back<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Believe it or not, that\u2019s all we have to do. Run the script and you should get back a list of empty folders:<\/P><PRE class=\"codeSample\">C:\\Documents and Settings\\Test\\Local Settings\\Temp\nC:\\Documents and Settings\\Test\\NetHood\nC:\\Documents and Settings\\Test\\PrintHood\nC:\\drvrtmp\nC:\\Inetpub\\wwwroot\\aspnet_client\\system_web\\2_0_50727\nC:\\Program Files\\Accessories\nC:\\Program Files\\Accessories\\Imagevue\nC:\\Program Files\\CA\\eTrust Antivirus\\ArcTemp\n<\/PRE>\n<P>Notice that we have C:\\Program Files\\Accessories <I>and<\/I> C:\\Program Files\\Accessories\\Imagevue. That\u2019s a sign that we\u2019re retrieving <I>all<\/I> the subfolders, and not just the top-level ones.<\/P>\n<P>As an added bonus, it turns out that you can even use this script against remote computers, provided that you use administrative shares like C$. If that\u2019s the case, then the following script will return a list of all the empty folders found on drive C of the computer atl-fs-01:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set FSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nShowSubfolders FSO.GetFolder(&#8220;\\\\atl-fs-01\\C$&#8221;)<\/p>\n<p>Sub ShowSubFolders(Folder)\n    For Each Subfolder in Folder.SubFolders\n    If Subfolder.Size = 0 Then\n        Wscript.Echo Subfolder.Path\n    End If\n        ShowSubFolders Subfolder\n    Next\nEnd Sub\n<\/PRE>\n<P>So is this the end of today\u2019s column? Well, we\u2019re not sure. After all, to paraphrase Zeno\u2019s Paradox, before you can reach the end of the column you have to reach the halfway point of the column. But before you can reach the halfway point of the column you have to reach the halfway point of the halfway point. But before you can reach &#8211; well, the gist of the paradox is that you never, <I>ever<\/I> reach the end of the column. <\/P>\n<P>Which is probably the case for a lot of our befuddled readers.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I list all the empty folders (that is, folders with a size of 0 bytes) on a specified drive?&#8212; DD Hey, DD. You know, physicist Edwin Schrodinger once proposed a thought experiment in which a cat is placed in a sealed box, along with a canister of poison gas. The [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[11,3,12,5],"class_list":["post-66943","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I list all the empty folders (that is, folders with a size of 0 bytes) on a specified drive?&#8212; DD Hey, DD. You know, physicist Edwin Schrodinger once proposed a thought experiment in which a cat is placed in a sealed box, along with a canister of poison gas. The [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66943"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66943\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}