How Can I Locate All the Folders That Have ArchiveASN in the Path?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I locate all the folders on a computer that have \Archive\ASN\ somewhere in the path?

— AB

SpacerHey, Scripting Guy! AnswerScript Center

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.

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.

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.

What’s that? How much will you get for doing this? Well, to tell you the truth, we didn’t think about that; it never occurred to us that someone might actually fall for this. Tell you what, AB: if you can figure out a way to smuggle $50 million out of Freedonia we’ll let you keep $43 million; all we ask is that the rest be divided equally among the Scripting Guys:

$1 million for Greg

$1 million for Jean

$1 million for Greg

$1 million for Dean

$1 million for Greg

$1 million for Peter

$1 million for Greg

Seems fair enough.

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 \Archive\ASN\ somewhere in the path.

In other words, a script like this one:

strComputer = “.”

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

Set colFolders = objWMIService.ExecQuery _ (“Select * From Win32_Directory Where Path Like ‘%\\Archive\\ASN\\%'”)

For Each objFolder in colFolders Wscript.Echo “Name: ” & objFolder.Name Next

Before we go any further we have something to confess: this script only works on Windows XP or Windows Server 2003. That’s because it uses WMI’s Like operator, an operator available only on those two operating systems. However, before we sign off for the day, we’ll show you a “brute force” script that will work on other versions of Windows.

And don’t worry; we won’t forget. After all, if you can’t trust the illegitimate children of the former defense minister of Freedonia who can you trust?

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:

Set colFolders = objWMIService.ExecQuery _
    (“Select * From Win32_Directory Where Path Like ‘%\\Archive\\ASN\\%'”)

As you can see, what we’re doing here is querying the Win32_Directory class, asking for a list of all the folders that have \\Archive\\ASN\\ somewhere in the path. (Don’t worry; we’ll 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 (%). In the wild and wacky world of WMI queries, the percent sign is a wildcard character meaning “any character or set of characters;” that means it serves the same function as the asterisk does in a command like this:

dir *.*

By putting percent signs before and after the target string we’re essentially saying, “Find any folder that has \\Archive\\ASN\\ somewhere in its path. We don’t care what characters, if any, come before the target string, and we don’t care what characters, if any, come after the target string. Just make sure that \\Archive\\ASN\\ shows up somewhere.”

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 “escaped” any time it’s 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:

\\Archive\\ASN\\

All that’s left to do now is set up a For Each loop to walk through the collection and echo back the Name of each folder that has \Archive\ASN\ somewhere in its path:

For Each objFolder in colFolders
    Wscript.Echo “Name: ” & objFolder.Name
Next

As we noted, if you’re running a version of Windows that shipped prior to Windows XP this script won’t do you any good; that’s because the version of WMI that came with those operating systems doesn’t support the Like operator. However, you can achieve the same net effect by using the following script, which returns a collection of all the folders on the computer, and then uses the InStr function to see if the string \Archive\ASN\ can be found anywhere within the folder path:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFolders = objWMIService.ExecQuery _ (“Select * from Win32_Directory”)

For Each objFolder in colFolders If InStr(objFolder.Path, “\Archive\ASN\”) Then Wscript.Echo “Name: ” & objFolder.Name End If Next

And you’re right: here we used the string \Archive\ASN\ instead of \\Archive\\ASN\\. That’s because we only need to escape the \ character when that character appears in a WQL query.

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 is on vacation. But it’s just a coincidence that we waited until she left town before trying to con – um, trying to recruit people to help us retrieve our $50 million

0 comments

Discussion is closed.

Feedback usabilla icon