Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I rename files like _JKG1234.jpg to 272_DSC_1234.jpg?

-- NA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, NA. You know, NA, we feel sorry for you. Why? Well, on the one hand you’re unbelievably lucky. After all, the Scripting Guys never write scripts that they actually use, yet you somehow managed to hit upon an exception. Talk about defying the odds! At the same time, however, you’re unbelievably unlucky: after all, the forces of the cosmos were perfectly aligned for you, yet you used up all that good karma on sending a question to the Scripting Guys instead buying yourself a lottery ticket. Ouch!

Two years ago one of the Scripting Guys went to Japan to watch his son play baseball. (Let’s face it, there’s a baseball angle to almost everything this Scripting Guy does.) While in Japan our Scripting Guy took a bunch of digital photos, photos that were automatically assigned names like 100_1023.jpg and 100_1024.jpg. This Scripting Guy was way too lazy to rename each and every photo, but he did think it might be useful if he at least modified the name to make it clear that the picture was part of the Japanese baseball collection. In other words, he wanted to take a photo named 100_1023.jpg and rename it Japan_1023.jpg. And, believe it or not, he actually used a script to perform this task.

And you guys thought the Scripting Guys talked the talk without walking the walk.

Not this Scripting Guy, though (or at least not on this one occasion). Instead, he used a script very similar to this one to rename his picture files:

strComputer = "."

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

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Photos'} Where " _
        & "ResultClass = CIM_DataFile")

For Each objFile In colFiles
    strEnd = Right(objFile.Name, 8)
    strNewName = objFile.Drive & objFile.Path & "272_DSC_" & strEnd
    errResult = objFile.Rename(strNewName)
Next

A couple things to note here. First, we’re assuming that you want to rename all the files in a particular folder. If that’s not the case, then you’ll have to include additional code to filter out files that shouldn’t be renamed. Second, we’re assuming that each photo ends with four digits and a .jpg file extension. If that’s not true, (for example, if you have files with names like _JKG1.jpg), well, then this particular script won’t help you. You can still rename files that have names like that, but the code is a bit more complicated. But seeing as how all the digital cameras we’re familiar with tack a four-digit number at the end of the file name, we’ll assume that’s true in your case as well.

The script begins by connecting to the WMI service on the local computer, then uses an Associators Of query to return a collection of all the files in the folder C:\Photos:

Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\Photos'} Where " _
        & "ResultClass = CIM_DataFile")

Admittedly, an Associators Of query is weird-looking. But don’t worry about that; instead, just replace C:\Photos with the path to your picture files and leave everything else alone.

We then use this block of code to walk through the collection of picture files:

For Each objFile In colFiles
    strEnd = Right(objFile.Name, 8)
    strNewName = objFile.Drive & objFile.Path & "272_DSC_" & strEnd
    errResult = objFile.Rename(strNewName)
Next

Inside the loop we take a look at the Name of the first file; that’s going to be something like C:\Photos\_JKG1234.jpg. (In WMI, the Name property of a file is what we would call the file path.) We then use the VBScript Right function to grab the rightmost eight characters in the Name; in this case, that will be 1234.jpg (C:\Photos\_JKG1234.jpg). Those are eight characters we want to preserve (we don’t want to change the numbering or the file extension), so we stash them in a variable named strEnd.

Next we construct a new name for our file; that’s what we do here:

strNewName = objFile.Drive & objFile.Path & "272_DSC_" & strEnd

When you rename a file using WMI you need to specify the complete file path; you can’t just rename a file something like NewName.jpg. Because of that we need to concatenate the drive letter for the file (C:), the path (in WMI terms, the folder path without the drive letter, or \Photos\), the new file name prefix (272_DSC_), and the eight characters we preserved from the existing file name. In other words:

C: + \Photos\ + 272_DSC_ + 1234.jpg

Put them all together and they spell C:\Photos\272_DSC_1234.jpg. And that’s good, because that’s exactly what we want the new file path to be.

From there we simply call the Rename method, passing the new file path as the sole method parameter. We then loop around and repeat the process for the remaining files in the collection.

And you know what, NA? We agree with you: knowledge like this is far more valuable than a winning lottery ticket. Just keep telling yourself that: far more valuable.