Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I add a suffix to a folder name? For example, I’d like to rename the folder C:\January to C:\January_2006.

-- NB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, NB. You know, back in July the Scripting Guy who writes this column happened to wander through the living room while the Scripting Son was watching the world-famous Nathan’s Hot Dog-Eating Contest on ESPN. (And no, don’t worry: this isn’t the start of some long and pointless rant against eating contests being broadcast on a sports channel. After all, if ESPN is going to show spelling bees, poker games, and soccer, well, why not hot dog-eating contests, too?) The Scripting Guy who writes this column watched for a few minutes and, truth be told, he was a bit repulsed by what he saw. Good heavens, he thought, how could people sit there and continually, and mindlessly, stuff themselves full of food like that? What is wrong with those people?!?

Of course, that was this summer, long before the Thanksgiving holiday. Now, after four days of turkey, mashed potatoes and gravy, sweet potatoes, stuffing, rolls, stir-fried green beans and bacon, assorted Jellos and salads, pumpkin pie, apple-raspberry pie – well, let’s just say that the Scripting Guy who writes this column now has a lot more empathy for people who mindlessly stuff themselves full of food.

Note. What’s that? What was the Scripting Guy who writes this column thankful for this past holiday season? Mainly he was thankful that he has a job where he can just sit all day and not have to move very much.

But don’t worry: he’ll be up and ready to go come time for Christmas dinner.

Fortunately, the Scripting Guy who writes this column doesn’t have to move very much in order to answer your question, NB. You need a script that can add a suffix to a folder name? Here you go:

strComputer = "."

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

Set colFolders = objWMIService.ExecQuery _
    ("Select * From Win32_Directory Where Name = 'C:\\January'")

For Each objFolder in colFolders
    strNewName = objFolder.Name & "_2006"
    objFolder.Rename strNewName
Next

Yes, this is a very simple script; what else would you expect for a column being written shortly after Thanksgiving? As you can see, we begin by connecting to the WMI service on the local computer, although – as with nearly all WMI scripts – we could just as easily run this script against a remote machine. (Meaning, in other words, that you can add a suffix to a folder found on a remote computer.) After making this connection we then use this line of code to retrieve a collection of all the folders on the computer that have a Name (which, in WMI-speak, is equivalent to the file path) equal to C:\January:

Set colFolders = objWMIService.ExecQuery _
    ("Select * From Win32_Directory Where Name = 'C:\\January'")

And no, we didn’t eat too much cranberry, walnut, and apple salad this weekend. Well, OK, actually we pretty much ate all of it, leaving only a spoonful or two for the rest of the family. However, that isn’t why we typed the name like this: C:\\January. Instead, the two \’s are actually required; that’s because the \ is a reserved character in WMI, and any time you use a reserved character in a WMI query you need to “escape” that character by prefacing it with a \. What if we wanted to rename the folder C:\January\Personnel\Reviews? Then we’d use a query like this one, taking care to escape each \ in the path:

Set colFolders = objWMIService.ExecQuery _
    ("Select * From Win32_Directory Where Name = 'C:\\January\\Personnel\\Reviews'")

After executing the query we’ll get back a collection consisting of all the folders on the computer that have the Name C:\January. (And, because folder Names – or paths – must be unique, we know the collection will contain, at most, one item.) With the collection in hand we next set up a For Each loop to loop through each of the folders in that collection. Inside that loop, we construct a new name for the folder by taking the existing folder Name and adding the string value _2006, storing the new path (C:\January_2006) in a variable named strNewName:

strNewName = objFolder.Name & "_2006"

Once we’ve constructed the new name we can then use the Rename method to rename C:\January, making sure that we pass the value strNewName as the method parameter:

objFolder.Rename strNewName

And yes, in order to rename a folder you must pass the entire path for the new folder. Passing just the folder name (January_2006) won’t do you any good.

Because we probably should try to get a little exercise we decided to toss in a bonus script. Suppose you have a folder – C:\Budget – that contains a bunch of subfolders (January, February, March, etc.). What if you wanted to tack a suffix onto each of those subfolders? No problem; this little script (which we won’t discuss in any detail today) should do the trick:

strComputer = "."

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

strFolderName = "C:\Budget"

Set colFolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")

For Each objFolder in colFolders
    strNewName = objFolder.Name & "_2006"
    objFolder.Rename strNewName
Next

Wow; that was more tiring than we expected it to be.

Incidentally, we know that many of you are dying to know who won this year’s Terducken Bowl. Unfortunately, we only managed to get halfway to 100: the game is currently tied at 49-49, and will have to be completed at Christmas.

So why weren’t we able to get in a complete game? Well, to begin with, two of the players (the Scripting Nephews) were late because they had to help clean house. (We were shocked, too: on the day of last year’s Super Bowl did the Pittsburgh Steelers make Ben Roethlisberger and Jerome Bettis clean their houses before letting them leave for the game?) On top of that, the game was played in a hurricane, with winds gusting around 250,000 miles per hour. And no, that’s not an exaggeration: at one point one of the Scripting Nephews threw a pass directly down the middle of the field. A huge gust of wind came up and not only blew the ball off course, it blew the ball completely off the field, over the fence and on into the neighbor’s yard.

But that’s OK; if nothing else, this gives the Scripting Guy who writes this column something to look forward to come Christmas time. What’s that? What about the food? Oh, sure, he’ll probably eat something. But just to be polite; after all, he wouldn’t want to hurt the Scripting Mom’s feelings. Maybe just a little turkey, a little gravy, and a tiny sliver of pie.

OK, maybe two tiny slivers of pie.