{"id":63293,"date":"2008-01-14T21:49:00","date_gmt":"2008-01-14T21:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/01\/14\/hey-scripting-guy-how-can-i-search-for-a-folder-and-return-the-folder-path\/"},"modified":"2008-01-14T21:49:00","modified_gmt":"2008-01-14T21:49:00","slug":"hey-scripting-guy-how-can-i-search-for-a-folder-and-return-the-folder-path","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-search-for-a-folder-and-return-the-folder-path\/","title":{"rendered":"Hey, Scripting Guy! How Can I Search For a Folder and Return the Folder Path?"},"content":{"rendered":"<p><img decoding=\"async\" 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\" \/> <\/p>\n<p>Hey, Scripting Guy! How can I search a computer for a folder, and return the folder\u2019s complete path?<\/p>\n<p>&#8212; AW<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\" \/><img decoding=\"async\" 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 decoding=\"async\" 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> <\/p>\n<p>Hey, AW. We know a lot of you have been sitting on pins and needles, waiting for this announcement, so we won\u2019t waste any time: yes, the Scripting Son has officially been accepted by the University of Idaho. Granted, that was pretty much a foregone conclusion: although the Scripting Dad still finds this hard to believe, the Scripting Son (the same son who tried making instant oatmeal by <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept06\/hey0922.mspx\"><b>pouring cold milk on it<\/b><\/a>) actually has pretty good grades and pretty good SAT scores. And now it looks like he\u2019s well on his way to becoming an Idaho Vandal.<\/p>\n<p>Anyway, all that made for quite an emotional day. The Scripting Son was happy, and relieved that the waiting was over. The Scripting Son\u2019s mother cried; she can\u2019t stand the thought of her 6\u2019-1\u201d 180-pound baby moving to Idaho for the next four years. And the Scripting Son\u2019s father cried as well: he can\u2019t stand the thought of his money being sent to Idaho for the next four years. <\/p>\n<p>Quite a day.<\/p>\n<p>Of course, as exciting as <i>that<\/i> day might have been it can\u2019t hold a candle to today, not when it comes to overwhelming exhilaration and sheer delight. Why not? Because today is the day we show you how to search a computer for a folder and, when found, return the complete path to that folder!<\/p>\n<p>We\u2019ll wait until everyone has had a chance to calm down a little before we show you the script. Take a moment to catch your breath, drink a sip or two of water, and then we\u2019ll continue.<\/p>\n<p>OK, everyone seems to be ready now:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\n\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\n\nSet colFolders = objWMIService.ExecQuery _\n    (\"Select * from Win32_Directory Where FileName = 'Scripts'\")\n\nFor Each objFolder in colFolders\n    Wscript.Echo objFolder.Name\nNext\n<\/pre>\n<p>More fun than a roller coaster? Well, we can\u2019t argue with you there. But, then again, what <i>Hey, Scripting Guy!<\/i> column <i>isn\u2019t<\/i> more fun than a roller coaster?<\/p>\n<table id=\"EUD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td>\n<p><b>Note<\/b>. Oh, wait: <i>that\u2019s<\/i> what a roller coaster is? Hmmm, we thought it was something totally different. Never mind then; this script isn\u2019t anywhere <i>near<\/i> as much fun as a roller coaster. But at least you won\u2019t get motion sickness from it.<\/p>\n<p>Well, at least <i>most<\/i> of you won\u2019t get motion sickness from it.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>As for the script itself, it starts out by connecting to the WMI service on the local computer. Can we also use this script to search for a folder on a <i>remote<\/i> computer? Sure we can. (Unless connecting to a remote computer gives you motion sickness.) To do that, simply assign the name of the remote computer to the variable strComputer. For example, to search for a folder on the computer atl-fs-001 you\u2019d use this line of code:<\/p>\n<pre class=\"codeSample\">strComputer = \"atl-fs-001\"\n<\/pre>\n<p>After making the connection to the WMI service we then use the following query to retrieve a collection of all the folders that have a <b>FileName<\/b> equal to <i>Scripts<\/i>:<\/p>\n<pre class=\"codeSample\">Set colFolders = objWMIService. _\n    ExecQuery(\"Select * from Win32_Directory Where FileName = 'Scripts'\")\n<\/pre>\n<p>By the way, don\u2019t let the WMI property names throw you off here. As far as WMI is concerned, the name of a folder is officially known as the FileName. And the path to the folder? As we\u2019re about to see, <i>that\u2019s<\/i> known as the <b>Name<\/b>. And yes, we\u2019d criticize those names except that, considering the fact that we have a son named The Scripting Son, well \u2026.<\/p>\n<p>At any rate, the query returns a collection consisting of all the folders on the computer named Scripts. That means that all we have to do now is set up a For Each loop that loops through the collection and then echoes back the path for each folder. You know, a loop just like this one:<\/p>\n<pre class=\"codeSample\">For Each objFolder in colFolders\n    Wscript.Echo objFolder.Name\nNext\n<\/pre>\n<p>On our test machine, that\u2019s going to result in output like this:<\/p>\n<pre class=\"codeSample\">c:\\documents and settings\\gstemp\\desktop\\stuff\\monad\\scripts\nc:\\documents and settings\\gstemp\\my documents\\scripts\nc:\\program files\\adobe\\adobe indesign cs2\\presets\\scripts\nc:\\program files\\adobe\\photoshop elements 4.0\\shared_assets\\scripts\nc:\\program files\\common files\\microsoft shared\\web server extensions\\40\\admcgi\\scripts\nc:\\program files\\common files\\microsoft shared\\web server extensions\\40\\admisapi\\scripts\nc:\\program files\\gpmc\\scripts\nc:\\program files\\log parser 2.2\\samples\\scripts\nc:\\program files\\macromedia\\flash 8\\en\\first run\\html\\learning extensions srvr files\\scripts\nc:\\program files\\microsoft platform sdk\\samples\\sysmgmt\\msi\\scripts\nc:\\program files\\powershell community extensions\\scripts\nc:\\program files\\specopssoft\\specops command\\admin tools\\scripts\nc:\\scripts\nc:\\windows\\pchealth\\helpctr\\system\\scripts\nc:\\windows\\pchealth\\helpctr\\system_oem\\scripts\nd:\\keep these\\ad week webcast\\scripts\nd:\\keep these\\runomatic\\scripts\nd:\\keep these\\scripting week\\scripting week 3\\scripts\nd:\\keep these\\tweakomatic\\scripts\nd:\\scripts\n<\/pre>\n<table id=\"EFF\" 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>. Like we said, we have a son named The Scripting Son. Knowing that, does it really come as that big of a surprise that most of the folders on our computer are named Scripts?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>We should point out that this script <i>can<\/i> run a trifle slow; depending on the number and size of your hard disks it can take a minute or so to complete. (That\u2019s because, by default, the script searches all the disks on your computer.) One way you can speed this up is to limit the search to a particular drive. For example, if you know that the folder of interest can be found somewhere on drive D you can use this script to search just that drive:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\n\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\n\nSet colFolders = objWMIService.ExecQuery _\n    (\"Select * From Win32_Directory Where FileName = 'Scripts' AND Drive = 'D:'\")\n\nFor Each objFolder in colFolders\n    Wscript.Echo objFolder.Name\nNext\n<\/pre>\n<p>As you can see all we did was add a second condition to our Where clause; now we want to limit the returned data to folders that have a FileName equal to <i>Scripts<\/i><b>and<\/b> reside on <b>Drive<\/b><i>D:<\/i>. And did it make a difference? You bet it did; on our test machine, searching 13 gigabytes of data on drive D took only a few seconds<\/p>\n<p>Just for the heck of it, here\u2019s another variation. This modified script enables you to pass the name of the folder as a command-line argument for the script; in turn, the script will search for the specified folder on drive D:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\n\nstrFolder = Wscript.Arguments(0)\n\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\n\nSet colFolders = objWMIService.ExecQuery _\n    (\"Select * From Win32_Directory Where FileName = '\" &amp; strFolder &amp; \"' AND Drive = 'D:'\")\n\nFor Each objFolder in colFolders\n    Wscript.Echo objFolder.Name\nNext\n<\/pre>\n<p>All we did here was set the value of the variable strFolder to the first command-line argument passed when starting the script:<\/p>\n<pre class=\"codeSample\">strFolder = Wscript.Arguments(0)\n<\/pre>\n<p>We then modified the query so that it searches for the folder name stored in strFolder instead of a hard-coded value like <i>Scripts<\/i>:<\/p>\n<pre class=\"codeSample\">Set colFolders = objWMIService.ExecQuery _\n    (\"Select * From Win32_Directory Where FileName = '\" &amp; strFolder &amp; \"' AND Drive = 'D:'\")\n<\/pre>\n<p>To run this script, just start it from the command prompt using a command similar to this:<\/p>\n<pre class=\"codeSample\">myscript.vbs scripts\n<\/pre>\n<p>That\u2019s probably too <i>much<\/i> excitement for one day, but what the heck. You only live once, right?<\/p>\n<p>That should do it, AW; we hope that answers your question. We\u2019d also like to announce that today\u2019s column is dedicated to the hard-working, highly-professional, and completely dedicated folks at the University of Idaho\u2019s Financial Aid Department. These are the people who hand out scholarships, grants, and other monetary awards, and the parents of most Idaho-bound students see them as little more than indentured servants. (You\u2019d be surprised at the nasty things some of these parents say.) But not the Scripting Guy who writes this column. No, by contrast, he truly appreciates these outstanding individuals and the money that they hand out. <\/p>\n<p>Sorry; we seem to have a typographical error in that last sentence. That last sentence <i>should<\/i> read \u201c\u2026 truly appreciates these outstanding individuals and the work that they do.\u201d The fact that they hand out money that helps defray college expenses is of no importance to the Scripting Guy who writes this column whatsoever. The only thing that matters is that the Financial Aid Department is staffed by absolutely <i>wonderful<\/i> human beings. Wonderful and, might we add, generous. <\/p>\n<p>Or so we hope.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I search a computer for a folder, and return the folder\u2019s complete path? &#8212; AW Hey, AW. We know a lot of you have been sitting on pins and needles, waiting for this announcement, so we won\u2019t waste any time: yes, the Scripting Son has officially been accepted by 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-63293","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 search a computer for a folder, and return the folder\u2019s complete path? &#8212; AW Hey, AW. We know a lot of you have been sitting on pins and needles, waiting for this announcement, so we won\u2019t waste any time: yes, the Scripting Son has officially been accepted by the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63293","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=63293"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63293\/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=63293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}