{"id":64253,"date":"2007-08-11T01:23:00","date_gmt":"2007-08-11T01:23:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/08\/11\/how-can-i-determine-if-the-number-of-files-in-a-series-of-folders-exceeds-a-specified-value\/"},"modified":"2007-08-11T01:23:00","modified_gmt":"2007-08-11T01:23:00","slug":"how-can-i-determine-if-the-number-of-files-in-a-series-of-folders-exceeds-a-specified-value","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-if-the-number-of-files-in-a-series-of-folders-exceeds-a-specified-value\/","title":{"rendered":"How Can I Determine If the Number of Files in a Series of Folders Exceeds a Specified Value?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I determine if the number of files in a series of folders exceeds a specified value?<BR><BR>&#8212; DP <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, DP. Buongiorno. Le come sta? Tutto bene?<\/P>\n<P>What does all that mean? Io non so; Io non parlo Italiano. In other words: I don\u2019t know; I don\u2019t speak Italian.<\/P>\n<P>No, really: \u201cIo non so; Io non parlo Italiano\u201d actually <I>does<\/I> mean \u201cI don\u2019t know; I don\u2019t speak Italian.\u201d As for the first sentence, that can be translated (at least by us) as this: \u201cGood day. How are you? Everything OK?\u201d<\/P>\n<P>So why the sudden interest in Italian? Well, even as you\u2019re reading this the Scripting Guy who writes this column is getting ready to wing his way towards Italy, off for a 10-day vacation in Rome and Venice. A couple of years ago the Scripting Guy who writes this column escorted several family members to London and Paris. He had a great time, but found it absolutely exhausting trying to get assorted and diverse family members to agree on where to go, what to do, when to eat, etc. \u201cThat was fun,\u201d he said upon returning home. \u201cBut I\u2019m never doing <I>that<\/I> again.\u201d<\/P>\n<P>And now, needless to say, he\u2019s doing <I>that<\/I> again. And, along the way, he\u2019s also trying to learn a few words of Italian, the better not to look like a typical clueless American tourist. (Obviously he <I>is<\/I> a typical clueless American tourist. He just doesn\u2019t want to look like one.) <\/P>\n<P>Originally, the Scripting Guy who writes this column hoped to write today\u2019s entire column in Italian; as it turns out, however, the teach-yourself-Italian book he picked up at the bookstore doesn\u2019t include the Italian equivalent for phrases like \u201cUse an Associators Of query to retrieve a collection of all the subfolders in a specified folder.\u201d But maybe that\u2019s just as well; after all, this column can be difficult enough to understand when he writes it in English, let alone if he tried writing it in Italian.<\/P>\n<P>So, in English, here\u2019s a script that should solve your problem, DP:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colSubfolders = objWMIService.ExecQuery _\n    (&#8220;Associators of {Win32_Directory.Name=&#8217;C:\\Absentee Reports&#8217;} &#8221; _\n        &amp; &#8220;Where AssocClass = Win32_Subdirectory &#8221; _\n            &amp; &#8220;ResultRole = PartComponent&#8221;)<\/p>\n<p>For Each objFolder in colSubfolders\n    Set colFiles = objWMIService.ExecQuery _\n        (&#8220;ASSOCIATORS OF {Win32_Directory.Name='&#8221; &amp; objFolder.Name &amp; &#8220;&#8216;} Where &#8221; _\n            &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>    If colFiles.Count =&gt; 4 Then \n        Select Case colFiles.Count\n            Case 4\n                Wscript.Echo objFolder.Name &amp; &#8221; has 4 files in it.&#8221;\n            Case 5\n                Wscript.Echo objFolder.Name &amp; &#8221; has 5 files in it.&#8221;\n            Case 6\n                Wscript.Echo objFolder.Name &amp; &#8221; has 6 files in it.&#8221;\n            Case 7\n                Wscript.Echo objFolder.Name &amp; &#8221; has 7 files in it.&#8221;\n            Case 8\n                Wscript.Echo objFolder.Name &amp; &#8221; has 8 files in it.&#8221;\n        End Select\n    End If\nNext\n<\/PRE>\n<P>As you can see, we start out by connecting to the WMI service on the local computer. But have no fear: we can just as easily run this script against a remote computer. To do so, all we have to do is assign the name of the remote computer the variable strComputer, like this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>According to DP, one of their servers houses a folder (in our example, C:\\Absentee Reports) that features a subfolder for each employee in the organization. In other words, a folder structure similar to this one:<\/P><PRE class=\"codeSample\">C:\\Absentee Reports\nC:\\Absentee Reports\\Gail Erickson\nC:\\Absentee Reports\\Jonathan Hass\nC:\\Absentee Reports\\Ken Myer\nC:\\Absentee Reports\\Pilar Ackerman\n<\/PRE>\n<P>Each time an employee is absent from the job a new document detailing the absence is added to the appropriate subfolder; for example, if Ken Myer is absent a new document will be added to the subfolder C:\\Absentee Reports\\Ken Myer. What we need to do is look inside each of these subfolders, count the number of documents in each one, and then take action based on that number. And that\u2019s a good question: how in the world are we going to do all that?<\/P>\n<P>Well, for starters, we\u2019re going to use this line of code to return a collection of all the subfolders found in C:\\Absentee Reports:<\/P><PRE class=\"codeSample\">Set colSubfolders = objWMIService.ExecQuery _\n    (&#8220;Associators of {Win32_Directory.Name=&#8217;C:\\Absentee Reports&#8217;} &#8221; _\n        &amp; &#8220;Where AssocClass = Win32_Subdirectory &#8221; _\n            &amp; &#8220;ResultRole = PartComponent&#8221;)\n<\/PRE>\n<P>And no, that\u2019s not Italian; Italian \u2013 even the Scripting Guy who writes this column\u2019s Italian \u2013 would make more sense. Instead, this crazy-looking code is an <B>Associators Of<\/B> query and, for better or worse, it\u2019s <I>supposed<\/I> to look like that. But you don\u2019t have to worry about the details; just note that we\u2019re retrieving a collection of subfolders (instances of the <B>Win32_Subdirectory<\/B> class) for the parent folder C:\\Absentee Reports. Note, too that this returns only the \u201ctop-level\u201d subfolders; if you have nested subfolders (subfolders inside of subfolders) then you\u2019ll need to use a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct04\/hey1020.mspx\"><B>different approach<\/B><\/A> to get at all the subfolders and sub-subfolders.<\/P>\n<P>Once we issue our query and get back a collection of all the subfolders in C:\\Absentee Reports, our next step is to set up a For Each loop to walk through all the folders in that collection. The first thing we do inside that loop is issue yet another query, this one returning a collection consisting of all the files found in the first subfolder:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name='&#8221; &amp; objFolder.Name &amp; &#8220;&#8216;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>Yes, another crazy (<I>pazzesco<\/I>) Associators Of query. But let\u2019s assume that the first subfolder in our collection is a folder named <I>Gail Erickson<\/I>; that means that the folder\u2019s <B>Name<\/B> property (something we more commonly refer to as the file path) is equal to <I>C:\\Absentee Reports\\Gail Erickson<\/I>. That means that our Associators Of query is asking for all the instances of the <B>CIM_DataFile<\/B> class that are associated with (stored in) the folder C:\\Absentee Reports\\Gail Erickson (represented by the variable <B>objFolder.Name<\/B>). In other words, connect to the C:\\Absentee Reports\\Gail Erickson folder and give us back a collection of all the files (files being instances of the CIM_DataFile class) that you find there.<\/P>\n<P>The rest is easy (<I>facile<\/I>). All WMI collections have a property (<B>Count<\/B>) that tells us the number of items in the collection. We want to take a different action based on whether a folder has 4, 5, 6, 7, or 8 files in it; if a folder has <I>less<\/I> than 4 files in it then we don\u2019t want to do anything at all. Consequently, as soon as we get back the collection we examine the value of the Count property to see if that value is equal to or greater than 4:<\/P><PRE class=\"codeSample\">If colFiles.Count =&gt; 4 Then\n<\/PRE>\n<P>If it is, we then set up a Select Case statement based on the value of the Count property:<\/P><PRE class=\"codeSample\">Select Case colFiles.Count\n    Case 4\n        Wscript.Echo objFolder.Name &amp; &#8221; has 4 files in it.&#8221;\n    Case 5\n        Wscript.Echo objFolder.Name &amp; &#8221; has 5 files in it.&#8221;\n    Case 6\n        Wscript.Echo objFolder.Name &amp; &#8221; has 6 files in it.&#8221;\n    Case 7\n        Wscript.Echo objFolder.Name &amp; &#8221; has 7 files in it.&#8221;\n    Case 8\n        Wscript.Echo objFolder.Name &amp; &#8221; has 8 files in it.&#8221;\n    Case Else\n        Wscript.Echo objFolder.Name &amp; &#8221; has more than 8 files in it.&#8221;\nEnd Select\n<\/PRE>\n<P>As you can see, we aren\u2019t doing anything very fancy here. If, for example, the Count is equal to 4 we simply echo back a message stating that the folder has 4 files in it. That\u2019s not very exciting, but that\u2019s not the point; the point is to show you how you can determine the number of files in a folder and then take specific action based on the number of files. Needless to say, to do something a little fancier (and a little more meaningful), all you have to do is replace our sample code with something a little fancier (and a little more meaningful).<\/P>\n<P>From there we simply loop around and repeat the process with the next subfolder in the collection. When all is said and done we\u2019ll get back a report similar to this:<\/P><PRE class=\"codeSample\">C:\\Scripts\\Ken Myer has 6 files in it.\nC:\\Scripts\\Pilar Ackerman has 6 files in it.\n<\/PRE>\n<P>At that point we\u2019re done (<I>fatto<\/I>).<\/P>\n<P>Or at least we <I>think<\/I> that <I>fatto<\/I> means \u201cdone\u201d. No doubt the Scripting Son, who fancies himself as being a bit of a comedian, would consider <I>fatto<\/I> to be a description of the Scripting Dad\u2019s waistline instead.<\/P>\n<P>Right, Scripting Son. Ha-ha.<\/P>\n<P>We hope that answers your question, DP. We\u2019d actually love to stay and discuss this in more detail, but it\u2019s time for the Scripting Guy who writes this column to head to the airport (<I>aeroporto<\/I>). But don\u2019t worry: he\u2019ll make sure that there\u2019s a new <I>Hey, Scripting Guy!<\/I> each and every day while he\u2019s gone. Why is he doing all that work even though he\u2019s on vacation? That\u2019s easy: it\u2019s because he \u2013 hmmm .\u2026 <\/P>\n<P>You know, now that we think about it, there\u2019s really only one answer to that question: because the Scripting Guy who writes this column isn\u2019t particularly <I>astuto<\/I> (smart). <\/P>\n<P>As the Scripting Son (not to mention the Scripting Editor) could have told you years ago.<\/P>\n<P>Oh, well; que sera sera. Arrivederci, everyone!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine if the number of files in a series of folders exceeds a specified value?&#8212; DP Hey, DP. Buongiorno. Le come sta? Tutto bene? What does all that mean? Io non so; Io non parlo Italiano. In other words: I don\u2019t know; I don\u2019t speak Italian. No, really: \u201cIo [&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":[38,3,12,5],"class_list":["post-64253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine if the number of files in a series of folders exceeds a specified value?&#8212; DP Hey, DP. Buongiorno. Le come sta? Tutto bene? What does all that mean? Io non so; Io non parlo Italiano. In other words: I don\u2019t know; I don\u2019t speak Italian. No, really: \u201cIo [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64253","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=64253"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64253\/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=64253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}