Hey, Scripting Guy! How Can I Search For a Folder and Return the Folder Path?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I search a computer for a folder, and return the folder’s complete path?

— AW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AW. We know a lot of you have been sitting on pins and needles, waiting for this announcement, so we won’t 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 pouring cold milk on it) actually has pretty good grades and pretty good SAT scores. And now it looks like he’s well on his way to becoming an Idaho Vandal.

Anyway, all that made for quite an emotional day. The Scripting Son was happy, and relieved that the waiting was over. The Scripting Son’s mother cried; she can’t stand the thought of her 6’-1” 180-pound baby moving to Idaho for the next four years. And the Scripting Son’s father cried as well: he can’t stand the thought of his money being sent to Idaho for the next four years.

Quite a day.

Of course, as exciting as that day might have been it can’t 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!

We’ll 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’ll continue.

OK, everyone seems to be ready now:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery _
    ("Select * from Win32_Directory Where FileName = 'Scripts'")

For Each objFolder in colFolders
    Wscript.Echo objFolder.Name
Next

More fun than a roller coaster? Well, we can’t argue with you there. But, then again, what Hey, Scripting Guy! column isn’t more fun than a roller coaster?

Note. Oh, wait: that’s what a roller coaster is? Hmmm, we thought it was something totally different. Never mind then; this script isn’t anywhere near as much fun as a roller coaster. But at least you won’t get motion sickness from it.

Well, at least most of you won’t get motion sickness from it.

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 remote 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’d use this line of code:

strComputer = "atl-fs-001"

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 FileName equal to Scripts:

Set colFolders = objWMIService. _
    ExecQuery("Select * from Win32_Directory Where FileName = 'Scripts'")

By the way, don’t 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’re about to see, that’s known as the Name. And yes, we’d criticize those names except that, considering the fact that we have a son named The Scripting Son, well ….

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:

For Each objFolder in colFolders
    Wscript.Echo objFolder.Name
Next

On our test machine, that’s going to result in output like this:

c:\documents and settings\gstemp\desktop\stuff\monad\scripts
c:\documents and settings\gstemp\my documents\scripts
c:\program files\adobe\adobe indesign cs2\presets\scripts
c:\program files\adobe\photoshop elements 4.0\shared_assets\scripts
c:\program files\common files\microsoft shared\web server extensions\40\admcgi\scripts
c:\program files\common files\microsoft shared\web server extensions\40\admisapi\scripts
c:\program files\gpmc\scripts
c:\program files\log parser 2.2\samples\scripts
c:\program files\macromedia\flash 8\en\first run\html\learning extensions srvr files\scripts
c:\program files\microsoft platform sdk\samples\sysmgmt\msi\scripts
c:\program files\powershell community extensions\scripts
c:\program files\specopssoft\specops command\admin tools\scripts
c:\scripts
c:\windows\pchealth\helpctr\system\scripts
c:\windows\pchealth\helpctr\system_oem\scripts
d:\keep these\ad week webcast\scripts
d:\keep these\runomatic\scripts
d:\keep these\scripting week\scripting week 3\scripts
d:\keep these\tweakomatic\scripts
d:\scripts

Note. 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?

We should point out that this script can run a trifle slow; depending on the number and size of your hard disks it can take a minute or so to complete. (That’s 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:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery _
    ("Select * From Win32_Directory Where FileName = 'Scripts' AND Drive = 'D:'")

For Each objFolder in colFolders
    Wscript.Echo objFolder.Name
Next

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 Scriptsand reside on DriveD:. 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

Just for the heck of it, here’s 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:

strComputer = "."

strFolder = Wscript.Arguments(0)

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colFolders = objWMIService.ExecQuery _
    ("Select * From Win32_Directory Where FileName = '" & strFolder & "' AND Drive = 'D:'")

For Each objFolder in colFolders
    Wscript.Echo objFolder.Name
Next

All we did here was set the value of the variable strFolder to the first command-line argument passed when starting the script:

strFolder = Wscript.Arguments(0)

We then modified the query so that it searches for the folder name stored in strFolder instead of a hard-coded value like Scripts:

Set colFolders = objWMIService.ExecQuery _
    ("Select * From Win32_Directory Where FileName = '" & strFolder & "' AND Drive = 'D:'")

To run this script, just start it from the command prompt using a command similar to this:

myscript.vbs scripts

That’s probably too much excitement for one day, but what the heck. You only live once, right?

That should do it, AW; we hope that answers your question. We’d also like to announce that today’s column is dedicated to the hard-working, highly-professional, and completely dedicated folks at the University of Idaho’s 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’d 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.

Sorry; we seem to have a typographical error in that last sentence. That last sentence should read “… truly appreciates these outstanding individuals and the work that they do.” 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 wonderful human beings. Wonderful and, might we add, generous.

Or so we hope.

0 comments

Discussion is closed.

Feedback usabilla icon