How Can I Search for Folders That Have a Dot in Their Name?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I search for folders that have a dot in their name? For example, a folder like Administrator.name or Folder.old?

— DU

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DU. You know, if your intention was to trick the Scripting Guys (not that tricking the Scripting Guys would be anything to brag about), well, you almost succeeded. That’s because, upon first reading your question, we immediately starting planning an elaborate series of scripts that would use wildcard characters and the WMI LIKE operator to search for folders that had a dot somewhere in their name. Of course, the LIKE operator is supported only on Windows XP and Windows Server 2003, which meant we had to simultaneously develop an even more elaborate series of scripts to perform this same task on Windows 2000. This was going to be hard, but we knew that the end result would be the largest, most complicated, and most breathtaking script ever created.

As you might expect, after hundreds of hours of work and countless nights without sleep, we realized there was a much easier (albeit less breathtaking) way to do this:

strComputer = “.”

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

Set colFolders = objWMIService.ExecQuery _ (“Select * from Win32_Directory Where Extension <> ””)

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

You’re right: there’s not a wildcard character or a LIKE operator to be found in our script. Why not? Because it turns out that we don’t need them. That’s because any time there is a dot in a folder name WMI treats the value after the dot as a file extension. For example, with a folder named Administrator.old, Administrator is considered the file name and old is considered the file extension. How about a folder named Administrator.old folder? In that case, old folder is considered the file extension. Here’s another one: Administrator.old.folder. Turns out that, if you have two or more dots in the name, only the value that comes after the last dot – folder – is considered the file extension. Everything else (Administrator.old) makes up the file name.

Does that really matter to any of us? You bet it does. If a folder has a dot somewhere in its name (and, as we’ve seen, it doesn’t matter where that dot occurs) that means that the folder has a file extension. In turn, that means we can find all the folders that have a dot in their name simply by looking for all the folders that have a file extension.

And that’s exactly what we do in our script. We start off by connecting to the WMI service on the local computer, although you could also – um, that’s right, you could also run this script against a remote computer. Guess we must have mentioned that somewhere before, huh? At any rate, after we connect to the WMI service we then encounter this line of code:

Set colFolders = objWMIService.ExecQuery _
    (“Select * from Win32_Directory Where Extension <> ””)

This is the heart and soul of our solution. What we’re doing here is asking WMI to return all the instances of the Win32_Directory class that have a file extension of some kind. To do that we simply request all the instances of Win32_Directory where the value of the Extension property is not a blank string (that is, it has some kind of value). Because there will be a file extension only if there’s a dot somewhere in the name we know that any folder we get back will – um, that’s right, have a dot somewhere in its name.

Do we keep repeating ourselves over and over again? That must be because the Scripting Guy who edits this column is beginning to show her age a bit. Poor thing: it must be hard to grow old. (Editor’s Note: The editor will have to ask the writer about that, since the writer is much older. And, if for some reason you believe what you heard on TechNet Radio, that would put the Scripting Guy who writes this column somewhere in his 70s.)

At any rate, our query returns a list of all the folders (directories) that have a file extension. All we have to do then is set up a For Each loop to walk through that list. Inside that loop we do just one thing: we echo back the value of the folder’s Name property:

Wscript.Echo objFolder.Name

And what will that give us? That will give us output similar to this:

c:\program files\microsoft sdks\windows\v1.0\samples\web\iis\isapi_6.0
c:\program files\microsoft visual studio .net 2003
c:\program files\microsoft visual studio 8\sdk\v2.0
c:\program files\microsoft.net

Pretty cool, huh?

Now, as it turns out – and stop us if you’ve heard this one before – if a folder has a dot in its name that means it also has a file extension ….

0 comments

Discussion is closed.

Feedback usabilla icon