{"id":66443,"date":"2006-09-19T11:26:00","date_gmt":"2006-09-19T11:26:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/19\/how-can-i-locate-all-the-folders-that-have-archiveasn-in-the-path\/"},"modified":"2006-09-19T11:26:00","modified_gmt":"2006-09-19T11:26:00","slug":"how-can-i-locate-all-the-folders-that-have-archiveasn-in-the-path","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-locate-all-the-folders-that-have-archiveasn-in-the-path\/","title":{"rendered":"How Can I Locate All the Folders That Have ArchiveASN in the Path?"},"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 locate all the folders on a computer that have <B>\\Archive\\ASN\\<\/B> somewhere in the path?<BR><BR>&#8212; AB<\/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, AB. We hope this message finds you well. Perhaps you are surprised to hear from us like this; a mutual friend provided us with your name and assured us that you were a responsible and trustworthy person. <\/P>\n<P>Recently we Scripting Guys discovered that we are all the illegitimate children of the late Ken Myer, former defense minister for the country of Freedonia. As defense minister our father was permitted to retain a small portion of each defense contract as payment for the services he rendered. Being a simple and honest man, he managed to save 50 million American dollars, all deposited in the Second National Bank of Freedonia.<\/P>\n<P>Of course, as happens so often to defense ministers in Freedonia, our father was recently assassinated. Before he died he managed to contact all of us and told us about the money; unfortunately, he went to his reward before he was able to give us the PIN number. That is why we are turning to you, AB, to ask for your help in transferring this money to an American bank account.<\/P>\n<P>What\u2019s that? How much will <I>you<\/I> get for doing this? Well, to tell you the truth, we didn\u2019t think about that; it never occurred to us that someone might actually <I>fall<\/I> for this. Tell you what, AB: if you can figure out a way to smuggle $50 million out of Freedonia we\u2019ll let you keep $43 million; all we ask is that the rest be divided equally among the Scripting Guys:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>$1 million for Greg<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>$1 million for Jean<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>$1 million for Greg<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>$1 million for Dean<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>$1 million for Greg<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>$1 million for Peter<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>$1 million for Greg<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Seems fair enough.<\/P>\n<P>Originally we intended to provide you with a $10,000 advance, just so you would know we were serious about this. Unfortunately, though, the current political situation in Freedonia makes it impossible for us to return home and withdraw the money. However, we do have something which many people would say is worth far more than $10,000: a script that can locate all the folders on a computer that have <B>\\Archive\\ASN\\<\/B> somewhere in the path.<\/P>\n<P>In other words, a script like this one:<\/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 colFolders = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Directory Where Path Like &#8216;%\\\\Archive\\\\ASN\\\\%'&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    Wscript.Echo &#8220;Name: &#8221; &amp; objFolder.Name\nNext\n<\/PRE>\n<P>Before we go any further we have something to confess: this script only works on Windows XP or Windows Server 2003. That\u2019s because it uses WMI\u2019s <B>Like<\/B> operator, an operator available only on those two operating systems. However, before we sign off for the day, we\u2019ll show you a \u201cbrute force\u201d script that will work on other versions of Windows. <\/P>\n<P>And don\u2019t worry; we won\u2019t forget. After all, if you can\u2019t trust the illegitimate children of the former defense minister of Freedonia who <I>can<\/I> you trust?<\/P>\n<P>As for the Windows XP\/Windows Server 2003 script, we start out by connecting to the WMI service on the local computer (although we could just as easily retrieve this same information from a remote computer). We then issue the following query:<\/P><PRE class=\"codeSample\">Set colFolders = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Directory Where Path Like &#8216;%\\\\Archive\\\\ASN\\\\%'&#8221;)\n<\/PRE>\n<P>As you can see, what we\u2019re doing here is querying the <B>Win32_Directory<\/B> class, asking for a list of all the folders that have <B>\\\\Archive\\\\ASN\\\\<\/B> somewhere in the path. (Don\u2019t worry; we\u2019ll explain that in a second.) Notice the syntax we use in this query. To begin with, we use the Like operator rather than, say, an equals sign. In addition, we surrounding the target string with percent signs (<B>%<\/B>). In the wild and wacky world of WMI queries, the percent sign is a wildcard character meaning \u201cany character or set of characters;\u201d that means it serves the same function as the asterisk does in a command like this:<\/P><PRE class=\"codeSample\">dir *.*\n<\/PRE>\n<P>By putting percent signs before and after the target string we\u2019re essentially saying, \u201cFind any folder that has \\\\Archive\\\\ASN\\\\ somewhere in its path. We don\u2019t care what characters, if any, come before the target string, and we don\u2019t care what characters, if any, come after the target string. Just make sure that \\\\Archive\\\\ASN\\\\ shows up <I>somewhere<\/I>.\u201d<\/P>\n<P>Now, why \\\\Archive\\\\ASN\\\\ rather than \\Archive\\ASN\\? Well, as it turns out, the \\ is a reserved character in WMI. Among other things, that means this character must be \u201cescaped\u201d any time it\u2019s used in a WMI query. How do you escape a character in WMI? Oddly enough, you put a \\ in front of it. Therefore, if we have a path like \\Archive\\ASN\\ that means we need to escape each and every \\; in turn, that gives us a path that looks like this:<\/P><PRE class=\"codeSample\">\\\\Archive\\\\ASN\\\\\n<\/PRE>\n<P>All that\u2019s left to do now is set up a For Each loop to walk through the collection and echo back the <B>Name<\/B> of each folder that has \\Archive\\ASN\\ somewhere in its path:<\/P><PRE class=\"codeSample\">For Each objFolder in colFolders\n    Wscript.Echo &#8220;Name: &#8221; &amp; objFolder.Name\nNext\n<\/PRE>\n<P>As we noted, if you\u2019re running a version of Windows that shipped prior to Windows XP this script won\u2019t do you any good; that\u2019s because the version of WMI that came with those operating systems doesn\u2019t support the Like operator. However, you can achieve the same net effect by using the following script, which returns a collection of <I>all<\/I> the folders on the computer, and then uses the <B>InStr<\/B> function to see if the string \\Archive\\ASN\\ can be found anywhere within the folder path:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFolders = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Directory&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    If InStr(objFolder.Path, &#8220;\\Archive\\ASN\\&#8221;) Then\n        Wscript.Echo &#8220;Name: &#8221; &amp; objFolder.Name\n    End If\nNext\n<\/PRE>\n<P>And you\u2019re right: here we used the string \\Archive\\ASN\\ instead of \\\\Archive\\\\ASN\\\\. That\u2019s because we only need to escape the \\ character when that character appears in a WQL query. <\/P>\n<P>That should do the trick, AB; if you have any questions, just let us know. And yes, now that you mention it, the Scripting Editor still <I>is<\/I> on vacation. But it\u2019s just a coincidence that we waited until she left town before trying to con \u2013 um, trying to <I>recruit<\/I> people to help us retrieve our $50 million<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I locate all the folders on a computer that have \\Archive\\ASN\\ somewhere in the path?&#8212; AB Hey, AB. We hope this message finds you well. Perhaps you are surprised to hear from us like this; a mutual friend provided us with your name and assured us that you were a [&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,4,12,5,6],"class_list":["post-66443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I locate all the folders on a computer that have \\Archive\\ASN\\ somewhere in the path?&#8212; AB Hey, AB. We hope this message finds you well. Perhaps you are surprised to hear from us like this; a mutual friend provided us with your name and assured us that you were a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66443","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=66443"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66443\/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=66443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}