How Can I Change the File Extension for All the Files in a Folder?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! We backed up a whole bunch of files in a folder; when these files were backed up the word “old” was inserted as part of the file extension (e.g., Filename.exe became Filename.oldexe). How can I restore these files to their original names and extensions?

— RP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RP. Today is Friday, June 1st, which can only mean one thing: the Scripting Guys are rushing around trying to tie up loose ends before they pack their bags and head for TechEd 2007. If you’re going to be at TechEd we’d be tickled pink if you’d drop by the CMP Media Booth (booth 1301) in the Partners Expo Hall and say hi. And because we know that no one would ever say hi to the Scripting Guys without getting something in return, well, we’ll be giving away copies of Dr. Scripto’s Fun Book and give you a chance to win a Dr. Scripto bobblehead doll to boot.

Note. OK, fine. Technically, you don’t even have to say hi: just swing by, grab the fun book, fill out the card for the drawing, and then you can leave. You don’t even need to make eye contact.

Now, we realize not everyone is going to TechEd. So does that mean that the moment the Scripting Guys set foot in Orlando they’ll forget all about the people who are not enjoying the Florida sunshine? Probably. But don’t despair: we’ve prepared a whole bunch of cool new stuff to keep you entertained (and informed) while we’re out riding amusement park rides. In addition to your daily dose of Hey, Scripting Guy! we’ll also have some new Windows PowerShell information; we’ll have a two-part article on burning CDs in Windows Vista; and we’ll even have an opportunity for you non-TechEd types to win a Dr. Scripto bobblehead. In other words, the Script Center isn’t closing just because the Scripting Guys will be out-of-town for a week; check the home page on Monday for more details.

Note. Oh, and if you could pick up our mail and water our plants while we’re gone we’d really appreciate it. Thanks!

In the meantime, one of those loose ends we need to tie up is today’s column. And there’s only one way to do that: come up with a script that can remove the word old from the file extension of all the files in a folder. You know, a script like this one:

strComputer = “.”

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

Set colFiles = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_Directory.Name=’C:\Test’} Where ” _ & “ResultClass = CIM_DataFile”)

For Each objFile In colFiles strExtension = objFile.Extension strExtension = Replace(strExtension, “old”, “”) strNewName = objFile.Drive & objFile.Path & objFile.FileName & “.” & strExtension errResult = objFile.Rename(strNewName) Next

Explain how the script works? No, we can’t do that this time around; after all, we do have a plane to catch. Sorry.

Oh; right. The Scripting Editor has … helpfully … reminded us that this is the morning of Friday, June 1st and the Scripting Guy who writes this column doesn’t actually leave until 10:50 PM on Sunday, June 3rd. So, what the heck; let’s explain how this script works.

To begin with, we connect to the WMI service on the local computer. And yes, this script will work just as well against a remote computer; all you have to do is assign the name of that remote machine to the variable strComputer. For example:

strComputer = “atl-fs-01”

After making the connection we then use an Associators Of query to return a collection of all the files (all instances of the CIM_DataFile class) found in the folder C:\Test:

Set colFiles = objWMIService.ExecQuery _
    (“ASSOCIATORS OF {Win32_Directory.Name=’C:\Test’} Where ” _
        & “ResultClass = CIM_DataFile”)

And yes, that’s definitely an odd-looking line of code. For now, however, don’t worry too much about it. Just replace C:\Test with the name of your folder and everything will be fine.

Our next step is to set up a For Each loop to loop through all the items in the collection (that is, through all the items in the folder). The first thing we do in this loop is grab the existing file extension (stored in the Extension property) and assign that value to a variable named strExtension:

strExtension = objFile.Extension

In WMI, the file extension does not include the period; that means that if our first file is named Filename.oldexe, then the variable strExtension will be equal to this: oldexe.

That’s fine, except we want to get rid of the word old. How do we do that? Like this:

strExtension = Replace(strExtension, “old”, “”)

All we’re doing here is calling the Replace function and replacing old with nothing (“”). Guess what that makes strExtension equal to now? As always, you’re way ahead of us:

exe

Now that we’ve recovered the original file extension it’s time to rename the file. When you rename a file using WMI you must pass the entire file path as the new file name; in other words, we have to say C:\Test\Filename.exe and not just Filename.exe. Because of that, our next line of code builds the complete file path for us:

strNewName = objFile.Drive & objFile.Path & objFile.FileName & “.” & strExtension

What we’re doing here is using selected properties of the original file as well as our new file extension in order to construct the new file path. The Drive property, as you might expect, returns the drive where the file is stored. Thus:

C:

The Path property is a kind of oddball-little property that returns folder information minus the drive letter and the file name. In other words, if the path is C:\Test\Filename.exe then the Path property is equal to \Test\. Combined with the Drive property, that gives us a path (so far) equal to this:

C:\Test\

Next up is the FileName property, which is the name of the file minus the file extension (and, of course, minus the dot between the name and the extension). Thus:

C:\Test\Filename

And then last but surely not least, we tack on the dot and the file extension (the new and improved file extension, which is stored in the variable strExtension). That’s going to give us a brand-new file path:

C:\Test\Filename.exe

The minute we have the new file path we can then call the Rename method and rename the file:

errResult = objFile.Rename(strNewName)

From there we loop around and repeat the process with the next file in the collection.

So remember, if you’re going to be in Orlando June 4 through June 8 be sure and stop by the convention center and say hi to the Scripting Guys. You might as well; after all, what else is there to do in a place like Orlando? To tell you the truth, we can’t imagine you’d be able to find anything in Orlando that compares to the excitement of meeting the Scripting Guys.

What’s that? Watching the grass grow? Well, OK, we’ll have to give you that one. Watching paint dry? Well, we’ll give you that one, too. Watching – OK, we get the idea. But come by and say hi anyway. After all, saying hi to the Scripting Guys is better than a poke in the eye with a sharp stick.

Granted, not much better. But better.

0 comments

Discussion is closed.

Feedback usabilla icon