How Can I Add the Date a Picture Was Taken to the File Names of My Digital Photos?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I rename all the .JPG files in a folder so that the date the picture was taken is included in the file name?

— NH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, NH. Welcome to Hey, Scripting Guy!, the only daily scripting column on TechNet that is written without the use of steroids, amphetamines, human growth hormone, or other forms of artificial enhancement. Each morning the Scripting Guy who writes this column undergoes extensive drug testing; so far, every one of these tests has come back negative. Hey, Scripting Guy!; written the way Nature intended it to be written.

Interestingly enough, the Scripting Editor is continually dismayed by the fact that Hey, Scripting Guy! is written without the use of steroids, amphetamines, human growth hormone, or other forms of artificial enhancement. “This column is so boring,” she says. “You really ought to try something to liven it up a bit. Yesterday you started out the column by saying, in effect, ‘Well, I don’t have anything interesting to say today.’ No one wants to read stuff like that. If you won’t take steroids at least do something!”

Editor’s Note: We’d like to note here that the Scripting Editor does not in any way endorse the taking of any performance-enhancing, performance-reducing, or performance-neutral drugs. The Scripting Editor does, however, endorse writing columns that are interesting but will still manage to keep the lawyers (or worse yet, our management) from coming after us for talking about things like…well, just as an example…steroids and amphetamines.

Well, with all due respect to the Scripting Editor, we beg to differ. Liven it up a bit?!? As our devoted readers know, practically every column we write features an amusing anecdote about the Scripting Son and his baseball team. How much more lively can you get?

And even if he does occasionally have a day when he has nothing interesting to say, even if he occasionally has entire years when he has nothing interesting to say, well, the Scripting Guy who writes this column steadfastly refuses to take steroids, amphetamines, human growth hormone, or any other form of artificial enhancement. And why should he? After all, any time he needs a little excitement he can just crank out a script like this one:

On Error Resume Next

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

Set objConnection = CreateObject(“ADODB.Connection”) Set objRecordSet = CreateObject(“ADODB.Recordset”)

objConnection.Open “Provider=Search.CollatorDSO;Extended Properties=’Application=Windows’;”

objRecordSet.Open “SELECT System.ItemPathDisplay, System.Photo.DateTaken FROM SYSTEMINDEX Where System.ItemFolderPathDisplay = ‘C:\Test'”, _ objConnection

objRecordSet.MoveFirst

Do Until objRecordset.EOF strName = objRecordset.Fields.Item(“System.ItemPathDisplay”) arrName = Split(strName, “.”)

dtmPhotoDate = objRecordset.Fields.Item(“System.Photo.DateTaken”) strDay = Day(dtmPhotoDate) strMonth = Month(dtmPhotoDate) strYear = Year(dtmPhotoDate) strNewName = arrName(0) & “_” & strMonth & “_” & strDay & “_” & strYear & “.” & arrName(1)

objFSO.MoveFile strName , strNewName objRecordset.MoveNext Loop

Wow, our head is still spinning and our heart is still pounding from that script!

Give us a minute or two to come down off our high, and then we’ll explain how this script works. Before we do that, however, we also need to issue one quick caveat. The date a digital photo was taken is retained any time that file is saved to a disk drive; to find the date, open Windows Explorer or My Computer, right-click the file, click Properties, click the Summary tab, and then click the Advanced button. (In Windows Vista, right-click, select Properties, then select the Details tab.) That’s the good news. The bad news? There’s nothing built into the operating system (prior to Windows Vista, that is) that allows a script writer to access this information. You can use the FileSystemObject or WMI to determine the date that the file was created (that is, the date the file was saved to the drive) but there’s no way for you to determine the date that the picture was taken. None.

So then how did we manage to write a script that can determine the date that a digital photo was taken and then add that date to the file name? That’s easy: steroids.

No, wait; sorry. As it turns out, we didn’t use steroids; instead, we used Desktop Search 3.0. (We always get those two things confused.) Granted, Desktop Search 3.0 is a piece of software you’ll have to download, install, and configure before you can use it. (Unless you’re running Windows Vista; in that case, the technology is included in the operating system.) That can be a hassle, and we’re well aware that many people are reluctant to download and install new components such as this. On the other hand, Desktop Search 3.0 is pretty darn cool. And it’s the only way we know of to solve this problem.

We’re not going to discuss the Desktop Search technology in any detail today; for more information about the technology and how it works, see our article Seek and Ye Shall Find. Instead, we’ll simply note that Desktop Search indexes selected folders and data stores on your computer (and yes, you have full control over what gets indexed and what doesn’t), then exposes that information through ADO (ActiveX Data Objects). What does that mean? That means you can write a simple database script – and use a standard SQL query – to do all sorts of cool things. Like what? Well, like determine the date that a digital photo was taken.

So then how do we determine the date that a digital photo was taken? To begin with, we create an instance of the Scripting.FileSystemObject object, something we’ll use when it comes time to rename our files. We next create instances of the database objects ADODB.Connection and ADODB.Recordset, then use this line of code to the bind to the Desktop Search data store:

objConnection.Open “Provider=Search.CollatorDSO;Extended Properties=’Application=Windows’;”

Incidentally, all that initial setup and configuration is boilerplate; you can pretty much use the code exactly as-is in any script you write that uses Desktop Search. The part you’ll need to change, and the part you’ll need to focus on, is the SQL query:

objRecordSet.Open “SELECT System.ItemPathDisplay, System.Photo.DateTaken FROM SYSTEMINDEX Where System.ItemFolderPathDisplay = ‘C:\Test'”, _
    objConnection

Again, we won’t discuss this in much detail today. Suffice to say that we are requesting information from the Desktop Search system index. What information are we requesting? We’re asking for two property values: System.ItemPathDisplay (the file path) and System.Photo.DateTaken (the – well, you can figure out for yourself what this is). Of course, we don’t want to retrieve this information for every file on the computer (although we could do so if we did want to). Instead, we only want this information for the files in the folder C:\Test. That’s what our Where clause is for: it limits returned data to items that have a System.ItemFolderPathDisplay (folder path) equal to C:\Test.

When we issue the Open command we get back a recordset consisting of the file path and photo date values for all the files in the folder C:\Test. In order to add the photo date to each file name we need to loop through this set of files, something we do by setting up a Do Until loop that runs until the EOF (End of File )property is True:

Do Until objRecordset.EOF

So then what are we going to do in that recordset? Well, for starters, we’re going to grab the file path for the first file in the collection and store that value in a variable named strName:

strName = objRecordset.Fields.Item(“System.ItemPathDisplay”)

That means that strName is going to be equal to something like C:\Test\100_0011.JPG. We’ve decided to tack the photo date onto the end of the file name, giving our file a new name along the lines of C:\Test\100_0011_8_3_2007.JPG (assuming, of course, that the photo was taken on August 3, 2007). With that in mind, we use the Split function to take the file path and convert it to an array named arrName:

arrName = Split(strName, “.”)

By splitting on the dot (and, yes, we’re assuming there’s only one dot in the file name) we end up with an array consisting of the following elements:

C:\Test\100_0011

JPG

Is that really going to help us rename the file? We’ll find out in just a second.

First, however, we need to grab the date that the picture was taken and store it in a variable named dtmPhotoDate:

dtmPhotoDate = objRecordset.Fields.Item(“System.Photo.DateTaken”)

After that, we use the following lines of code (and the VBScript functions Day, Month, and Year) to parse out the day (3), month (8), and year (2007) from the picture date:

strDay = Day(dtmPhotoDate)
strMonth = Month(dtmPhotoDate)
strYear = Year(dtmPhotoDate)

Got all that? Good; then you shouldn’t have too much trouble understanding the following line of code:

strNewName = arrName(0) & “_” & strMonth & “_” & strDay & “_” & strYear & “.” & arrName(1)

All we’re doing here is constructing a new file path, a path consisting of the following elements:

C:\Test\100_0011 (item 0 in the array arrName)

_

8 (the value of the variable strMonth)

_

3 (the value of the variable strDay)

_

2007 (the value of the variable strYear)

.

JPG (item 1 in the array)

What does all that add up to? That all adds up to this: C:\Test\100_0011_8_3_2007.JPG. And yes, that is our original file path, plus the date that the photo was taken.

The rest is easy. To rename the file, we use the FileSystemObject and the MoveFile method, like so:

objFSO.MoveFile strName , strNewName

Admittedly, this is a funny way to rename files; instead of using a command like Rename we “move” the file from its old path (C:\Test\100_0011.JPG) to its new path (C:\Test\100_0011_8_3_2007.JPG). Not the most intuitive operation in the world, but, in the end, the file gets renamed, which is all we really care about.

From there we simply call the MoveNext method to move on to the next file in the recordset. And that, as they say, is that.

In the interests of full disclosure, we should note that the Scripting Guy who writes this column doesn’t really undergo drug testing each and every morning. (If he did, any tests for steroids, amphetamines, or human growth hormone would come back negative. However, any tests for doughnuts or maple bars would likely come back positive.) We realize that this is hard to believe; after reading some of our columns your first reaction is likely to be, “Man, what kind of drugs is this guy on?!?” But no, Hey, Scripting Guy! is drug-free. You know what they say: winners don’t use drugs.

Well, OK, maybe Tour de France winners do. But other winners don’t. And neither does the Scripting Guy who writes this column.

0 comments

Discussion is closed.

Feedback usabilla icon