How Can I Retrieve a List of All the Files That Have No File Extension?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I retrieve a list of all the files that have no file extension?

— JB

SpacerHey, Scripting Guy! AnswerTechNet Script Center

Hey, JB. Funny you should ask about no-hitters. Yes, it’s true: the Scripting Son was robbed of a no-hitter this past Saturday. With one out in the top of the seventh (you play just seven innings in Colt League baseball), the Scripting Son had a no-hitter going. The next batter dribbled a ball down the third base line. It was clearly a foul ball; at first the batter didn’t even run but just stood there and watched. Unfortunately, though, the home plate umpire didn’t see it that way. He called the ball fair and, after a mad scramble, the batter managed to get safely to first. Officially, it was a base hit and, officially, the no-hitter was over.

Note. Uh, no, it’s probably best if we don’t relate the opinions of the Scripting Son regarding this call. Let’s just say that he was happy the team won, and leave it at that.

What’s that? Yes, we noticed that the question said “no file extension” rather than “no-hitter.” We just assumed that was a typo. Surely no one could be thinking about work at a time like this. (The time, of course, being baseball season.)

On the other hand, it’s theoretically possible that someone might be more interested in a scripting than they are in hearing about the pitching exploits of the Scripting Son. (Unlikely, but still possible.) Just in case JB really did mean “no file extension,” here’s a script that will take care of that:

strComputer = “.”

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

Set colFiles = objWMIService.ExecQuery _ (“Select * from CIM_DataFile where Extension = ””)

For Each objFile in colFiles Wscript.Echo objFile.Name Next

Yes, it’s very easy. (After all, we’re entering the home stretch of the baseball season; we don’t have time to write long and complicated scripts!) As you can see, we begin by connecting to the WMI service on the local computer. Is it possible to connect to a remote computer and find all the files that don’t have file extensions? Of course it is: just assign the variable strComputer the name of the remote machine. For example, this modified line of code will cause the script to search the computer atl-fs-01 for files without file extensions (not to be confused with Doctors Without Borders, Parents Without Partners, or A Day Without Sunshine):

strComputer = “atl-fs-01”

After making the connection we then issue the following query:

Set colFiles = objWMIService.ExecQuery _
    (“Select * from CIM_DataFile where Extension = ””)

What we’re doing here is querying the CIM_DataFile class, the WMI class used for managing files. Of course, we don’t want a list of all the files on the computer, just those that don’t have a file extension. To do that, we simply tack on a Where clause that limits returned data to files where the Extension property is equal to nothing (that is, an empty string: ‘’).

Our query returns a collection of all the files that don’t have a file extension. To review that list we set up a For Each loop to walk through the collection, and then echo back the Name (path) for each file found there. It’s that easy.

Note. Could we echo back other file properties besides Name? Sure; see the Microsoft Windows 2000 Scripting Guide for details.

Here’s an equally sad story for you. Last Thursday one of our pitchers had a no-hitter after four innings. In the top of the fifth, the leadoff batter hit a ground ball right to the shortstop. Our shortstop took a little too much time, and then kind of lobbed the ball over to first. The batter beat it out for an infield single, the first – and only – hit the other team had all night. In other words, that’s twice in the span of just three days that we could have – and probably should have – had no-hitters. But we ended up with nothing at all.

Maybe we’ll give our pitchers a copy of the script that finds files that don’t have file extensions. That should cheer them up!


Discussion is closed.

Feedback usabilla icon