Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I delete all the .csg files on drive C of a computer?

-- DL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DL. You know, it’s funny how quickly things can change. Just a few hours ago the Scripting Guy who writes this column was in such a bad mood that he had no intention of explaining to you (or anyone else for that matter) how they could delete all the .csg files on drive C of a computer; in fact, the Scripting Guy who writes this column was already making plans to go live on a deserted island and never speak to anyone ever again.

Note. What’s that noise you hear in the background? Nothing, really; just the other Scripting Guys cheering. Sorry to disappoint you, guys; turns out he’s not going anywhere after all.

Oh, come on, Scripting Editor: there’s no need to start crying.

So what could possibly have caused the jovial and fun-loving Scripting Guy to turn so grouchy? Well, his evening actually started out quite nicely: there’s nothing better than hopping in the car with the Scripting Son and heading for a Husky basketball game. As they drove along, however, and as they dealt with the Seattle traffic, the Scripting Son made some clever remark about there being so many people on the road that they should just stay home and watch the game on TV. At that moment, the Scripting Guy who writes this column had a sudden – and horrifying – thought: the tickets to the game were still sitting on the counter at home.

You’re right: that was a problem. Fortunately, thanks to a U-turn and some questionable driving tactics, the pair managed to return home, get the tickets, and still reach their favorite Seattle sandwich shop in time to grab some quick dinner before the game. (After all, red lights and stop signs are really more of a suggestion than anything else.)

Or at least they thought they were about to grab a quick dinner: instead, they discovered that their favorite sandwich shop was closed, soon to be replaced by a sports bar. A sports bar?!? Come on: if you want to drink beer and watch sports on TV you can do that at home. But can you get a huge sandwich, with deli-style ham piled high on fresh-baked bread, at home? Not at the Scripting House, that’s for sure.

And then, to add insult to injury, the Huskies raced to an 11-point halftime lead against the Oregon Ducks (of all teams), then promptly lost that lead just a few minutes into the second half. “If they lose this game, on top of everything else that’s happened tonight, then I’m moving to a deserted island,” muttered the Scripting Guy who writes this column.

Note. You know, we didn’t really pay any attention at the time, but now that you mention it the Scripting Son didn’t try to talk his dad out of moving to a deserted island. Hmmm ….

Fortunately, though, the Huskies pulled themselves together and clobbered the Ducks 89-77. In turn, the Scripting Guy who writes this column cashed in his one-way ticket to the deserted island, then sat down and hammered out a script that can delete all the .csg files on drive C of a computer:

strComputer = "."

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

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile Where Drive = 'C:' and Extension = 'csg'")

For Each objFile in colFiles
    objFile.Delete
Next 

As you can see, this is a pretty simple little script. We begin by connecting to the WMI service on the local computer. Could we use this same script to delete .csg files from a remote computer? You bet; just assign the name of the remote computer to the variable strComputer, like so:

strComputer = "atl-fs-01"

After we make the connection to WMI we then issue this query to return a collection of all the .csg files found on drive C:

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile Where Drive = 'C:' and Extension = 'csg'")

Like most WMI queries, the secret here lies in the Where clause. Obviously we don’t want to return (and thus delete) all the files on the computer; instead, we only want to return and delete files where two things are true:

The file is on drive C.

The file has a .csg file extension.

And thus, our Where clause: Where Drive = 'C:' and Extension = 'csg'. Note two things here. First, the colon is part of the drive designation: it’s drive C: and not drive C. Second, in WMI the period is not considered part of the file extension. Thus we’re looking for the extension csg and not the extension .csg.

So what do we do with the collection of files returned by our query? That’s easy: we simply set up a For Each loop to loop through that collection; inside the loop, we use the Delete method to delete the files one-by-one. That’s what this little block of code does:

For Each objFile in colFiles
    objFile.Delete
Next

Believe it or not, that’s all we have to do. Run this little script and any .csg files on drive C (and only on drive C) will be deleted.

You know, that’s another good question. Considering the fact that the Huskies have been struggling with both injuries and inexperience this year, would it really have been that big of a deal had they lost to Oregon? After all, the Ducks did come into the game ranked as the seventh-best team in the entire country.

Let’s put it this way. Suppose that the Martians landed on Earth and challenged the planet to a basketball game. If the Earth team wins the Martians will go home and we’ll all be saved. If the Earth team loses, however, the Martians will destroy our planet and everything on it.

Got that? OK, now suppose that the Oregon Ducks have been chosen to represent the planet Earth. With the fate of all humanity on the line, who do you think the Scripting Guy who writes this column will be rooting for?

Hey, come on: the destruction of the Earth is a small price to pay in exchange for the joy of watching Oregon lose.