{"id":55763,"date":"2008-04-17T01:09:00","date_gmt":"2008-04-17T01:09:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/04\/17\/hey-scripting-guy-how-can-i-find-folders-whose-name-fits-a-specified-pattern\/"},"modified":"2008-04-17T01:09:00","modified_gmt":"2008-04-17T01:09:00","slug":"hey-scripting-guy-how-can-i-find-folders-whose-name-fits-a-specified-pattern","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-find-folders-whose-name-fits-a-specified-pattern\/","title":{"rendered":"Hey, Scripting Guy! How Can I Find Folders Whose Name Fits a Specified Pattern?"},"content":{"rendered":"<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/><\/p>\n<p>Hey, Scripting Guy! I have a bunch of folders that had names like this: BAD_20080411; in other words, the characters <i>BAD_ <\/i>followed by the date. These folders can be on any drive on my computer. How can I write a script that will locate all these folders for me?<br \/>&#8212; NS<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, NS. Did you hear about the construction worker who attempted to place a curse on the New York Yankees by burying a David Ortiz Boston Red Sox jersey under two-and-a-half feet of concrete in the new Yankee Stadium (currently under construction)? Your first thought upon hearing that might have been, &ldquo;So what?&rdquo; especially when you consider the fact that David Ortiz is currently batting .104, with just one home run and three RBIs. Nevertheless, the Yankees didn&rsquo;t find it so amusing, and spent an estimated $50,000 digging up the jersey.<\/p>\n<p>To their credit, the Yankees plan to auction off the jersey and donate the proceeds to the Jimmy Fund, the Red Sox&rsquo; favorite charity. Of course, the Yankees also plan to talk to the district attorney&rsquo;s office about the possibility of filing criminal charges against the construction worker who planted the jersey. As far as we know, the Yankees want to have the construction worker weighted down with stones and then tossed into the Hudson River. If he sinks and drowns, well, then the team will concede that no crime was committed after all. If the construction worker floats, however, he&rsquo;ll be hauled out and burned at the stake for being a witch.<\/p>\n<p>Which, interestingly enough, is how the Yankees got rid of their last two left fielders.<\/p>\n<p>We mention this not to poke fun at the Yankees; after all, when the Scripting Guy who writes this column was growing up his favorite player was Yankee centerfielder Bobby Murcer. Instead we consider this a public service: if you&rsquo;ve been having a streak of bad luck lately the Scripting Guys strongly recommend that you dig up your entire backyard. Let&rsquo;s face it: if there&rsquo;s a David Ortiz jersey buried in Yankee Stadium there could be a David Ortiz jersey buried just about anywhere.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EAD\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p><b>Baseball Trivia<\/b>. David Ortiz was once in the Seattle Mariners organization; in fact, in 1996 he hit .322 with 18 home runs and 93 RBIS for the Wisconsin Timber Rattlers, a Mariners farm team. As a reward for this outstanding season Ortiz was traded to the Minnesota Twins for Dave Hollins. Hollins appeared in just 28 games for the Mariners and then signed a free agent contract with the Angels. In the meantime, Ortiz has had 263 home runs and 883 RBIs for the Red Sox, who&rsquo;ve also won two World Series titles during his time with the team.<\/p>\n<p>Incidentally, that has nothing to do with being cursed: the Mariners are <i>always<\/i> making deals like that.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Admittedly, digging up your backyard might seem like a pretty drastic measure, especially if you <i>don&rsquo;t<\/i> find a David Ortiz jersey buried there. Alternatively, you can break a curse using this method, an approach we copied from an Internet newsgroup post:<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EVD\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p>Create a small circle of black salt, with Lavender incense burning to the left and the right of the Circle. Focus clearly on the intent that you want [to be rid of the curse]. A silvery ball should appear after 15 minutes. With your mind, push the ball towards [the person who cursed you], and within a week that should help out with your problem.<\/p>\n<p>Personally I would use a simple Mind Spell to remedy it, but this method will be just as effective.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>In other words, either approach &ndash; a circle of black salt or a simple Mind Spell &ndash; should do the trick. Or, you could also do what the Scripting Guys do any time they need to break a curse. (Which is more often that you might think, at least for people who work at Microsoft.) Any time someone places a curse on us we simply run a script that can locate all the folders on a computer whose folder name includes the characters <i>BAD<\/i>_ followed by a date:<\/p>\n<pre class=\"codeSample\">Set objRegEx = CreateObject(\"VBScript.RegExp\")<\/pre>\n<pre class=\"codeSample\">objRegEx.Global = True&nbsp;&nbsp; <br \/>objRegEx.IgnoreCase = True<br \/>objRegEx.Pattern = \"^bad_\\d{8}\"<\/pre>\n<pre class=\"codeSample\">strComputer = \".\" <\/pre>\n<pre class=\"codeSample\">Set objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")<\/pre>\n<pre class=\"codeSample\">Set colFolders = objWMIService.ExecQuery(\"Select * From Win32_Directory\")<\/pre>\n<pre class=\"codeSample\">For Each objFolder in colFolders<br \/>&nbsp;&nbsp;&nbsp; strFolder = objFolder.FileName<br \/>&nbsp;&nbsp;&nbsp; Set colMatches = objRegEx.Execute(strFolder)<\/pre>\n<pre class=\"codeSample\">&nbsp;&nbsp;&nbsp; If colMatches.Count &gt; 0 Then<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Wscript.Echo objFolder.Name <br \/>&nbsp;&nbsp;&nbsp; End If<br \/>Next <\/pre>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EEE\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. Will running this script <i>really<\/i> break a curse? Let&rsquo;s put it this way: Jean and Greg are both still Scripting Guys, aren&rsquo;t they?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Our script starts out by creating an instance of the <b>VBScript.RegExp<\/b> object, the object that enables us to use regular expressions within a VBScript script. (Does that mean we&rsquo;re going to use regular expressions in this script? You bet it does.) After we create the regular expressions object we set both the <b>Global<\/b> and <b>IgnoreCase<\/b> properties to True. That ensures that our script will do two things: 1) find all instances of the target text; and, 2) do a case-insensitive search for that target text. (In other words, the script will find folders starting with <i>BAD_<\/i>, with <i>bad_<\/i>, etc.)<\/p>\n<p>Speaking of the target text, we use the following line of code to define the <b>Pattern<\/b> we&rsquo;re looking for:<\/p>\n<pre class=\"codeSample\">objRegEx.Pattern = \"^bad_\\d{8}\"<\/pre>\n<p>As far as regular expressions go, this one is actually pretty straightforward. The syntax <b>^bad_<\/b> simply states that we are looking for the characters <i>bad_<\/i> at the <i>beginning<\/i> of the folder name. (The caret symbol &ndash; ^ &ndash; matches items at the beginning of a string.) The construction <b>\\d{8}<\/b> states that the characters <i>bad_<\/i> must then be followed by 8 consecutive digits (any of the numbers 0 through 9). The following folder name matches our pattern:<\/p>\n<pre class=\"codeSample\">Bad_20080411<\/pre>\n<p>However, this folder name does <i>not<\/i> match the pattern:<\/p>\n<pre class=\"codeSample\">Bad_1234567<\/pre>\n<p>Why not? You got it: because the characters <i>bad_<\/i> are followed by only <i>7<\/i> digits, not the required 8. This folder name won&rsquo;t result in a match, either:<\/p>\n<pre class=\"codeSample\">Test_bad_20080411<\/pre>\n<p>Why not? Right again: because <i>bad_20080411<\/i> does not appear at the beginning of the name.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EPG\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><b>Note<\/b>. We&rsquo;re glad to hear that the curse placed on you hasn&rsquo;t affected your ability to decipher regular expressions.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>After configuring the regular expressions object we then connect to the WMI service on the local computer. And yes, you <i>can<\/i> run this script against a remote computer; to do that, simply assign the name of the remote machine to the variable strComputer:<\/p>\n<pre class=\"codeSample\">strComputer = \"atl-fs-001\"<\/pre>\n<p>That should work just fine, assuming that there isn&rsquo;t a curse on the remote computer.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" class=\"dataTable\" id=\"EBH\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p><b>Note<\/b>. How could you even <i>put<\/i> a curse on a remote computer? Beats us; maybe by installing Linux on it?<\/p>\n<p>Hey, just kidding.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Once we&rsquo;re connected to the WMI service we can use this line of code to retrieve a collection of all the folders found on that computer:<\/p>\n<pre class=\"codeSample\">Set colFolders = objWMIService.ExecQuery(\"Select * From Win32_Directory\")<\/pre>\n<p>And yes, you&rsquo;re right: this script <i>could<\/i> take a minute (maybe even more) to complete. But that&rsquo;s probably <i>not<\/i> a sign that you&rsquo;ve been cursed; most likely it&rsquo;s because you have a large hard drive (or hard drives), and that you have a whole bunch of folders that need to be retrieved.<\/p>\n<p>Now, in an ideal world we&rsquo;d be able to use regular expressions right in our WMI query; that would enable us to retrieve only the folders of interest, without having to return the collection of <i>all<\/i> the folders on the computer. However, considering the fact that the Scripting Editor is still on staff, this is far from a perfect world. (Hey, just kidding, Scripting Editor; you know <i>exactly<\/i> how much we like you!) Because of that, our only recourse is to return a collection of all the folders and then look at each folder name individually, trying to determine whether the name meets the specified pattern.<\/p>\n<p>With that in mind the next thing we do is set up a For Each loop designed to walk through all the folders in our folder collection. Inside that loop, we grab the value of the <b>FileName<\/b> property and store it in a variable named strFolder:<\/p>\n<pre class=\"codeSample\">strFolder = objFolder.FileName<\/pre>\n<p>And yes, we know: <i>Folder<\/i>Name <i>would<\/i> be a better name for this property, wouldn&rsquo;t it? But, like we said, this is not an ideal world we&rsquo;re living in. After all, in an ideal world <i>Baseball Tonight<\/i> would be on more than just twice each night.<\/p>\n<p>Once we have the folder name in tow we can use the <b>Execute<\/b> method to run a regular expressions search against that name:<\/p>\n<pre class=\"codeSample\">Set colMatches = objRegEx.Execute(strFolder)<\/pre>\n<p>If the target text can be found in the folder name then all the instances of that target text will be stored in a collection we named colMatches. How do we know if a given folder name matches the specified Pattern? That&rsquo;s easy; we just check to see if the number of items in colMatches (the collection&rsquo;s <b>Count<\/b> property) is greater than 0:<\/p>\n<pre class=\"codeSample\">If colMatches.Count &gt; 0 Then<\/pre>\n<p>If the Count is greater than 0 then we&rsquo;ve found one of the folders we were looking for; in turn, we echo back the folder path (that is, the value of the <b>Name<\/b> property):<\/p>\n<pre class=\"codeSample\">Wscript.Echo objFolder.Name<\/pre>\n<p>And then, either way, it&rsquo;s back to the top of the loop, where we repeat the process with the next folder in the collection. When all is said and done, we should see information similar to the following onscreen:<\/p>\n<pre class=\"codeSample\">d:\\bad_20080411\nd:\\scripts\\bad_20080412\ne:\\bad_20080413<\/pre>\n<p>Etc., etc.<\/p>\n<p>Incidentally, David Ortiz is lucky it&rsquo;s just his <i>jersey<\/i> that was buried under two-and-half-feet of concrete. During the Dark Ages it was believed that &ndash; in order to keep the river peaceful and under control &ndash; any new bridge built over that river had to include a body entombed within the bridge. We&rsquo;re not sure whether that really did any good or not, but when the Scripting Guys world headquarters was being built we decided not to take any chances. Ever wonder why you haven&rsquo;t heard much from Scripting Guy Peter Costantini lately? Well, now you know why. <\/p>\n<p>See you all tomorrow.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a bunch of folders that had names like this: BAD_20080411; in other words, the characters BAD_ followed by the date. These folders can be on any drive on my computer. How can I write a script that will locate all these folders for me?&#8212; NS Hey, NS. Did you hear [&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-55763","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! I have a bunch of folders that had names like this: BAD_20080411; in other words, the characters BAD_ followed by the date. These folders can be on any drive on my computer. How can I write a script that will locate all these folders for me?&#8212; NS Hey, NS. Did you hear [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55763","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=55763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55763\/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=55763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}