Click here to Skip to main content
15,867,883 members
Articles / Desktop Programming / MFC

JPGDateChanger - Tool to Restore Last Modified Date for JPG Files

Rate me:
Please Sign up or sign in to vote.
4.96/5 (21 votes)
8 Nov 2012CPOL5 min read 177K   5.3K   52   78
JPGDateChanger is a tool that changes last modified date to the one stored in EXIF data

Image 1

Introduction

JPGDateChanger is a tool that changes file date of a JPG file to match the date extracted from Date Taken field in the file's EXIF tag.

Background

Often after transferring images from my digital camera, I quickly go through them and fix the orientation on pictures taken in portrait mode. On my XP machine, I simply use the built in Windows Picture and Fax Viewer to do the job. On my Vista machine, this program was replaced with Windows Photo Gallery. The new 'feature' of Vista's replacement is that clicking on Rotate button sets the "Date Modified" to current time. I find this behaviour really annoying, since I usually sort my pictures in the folder by date.

Another feature of Windows I found frustrating is when it stamps all the files with current date and time instead of original date if transferring images via USB cable instead of copying off media card directly.

After screwing up the dates on some pictures for the Nth time (being forgetful), I tried to find a tool on the Internet that would change the date back by extracting it from EXIF tag. Unfortunately I found only some all-in-one utilities, all bloated and all shareware.

Well, it shouldn't be too hard to write one myself, I thought. Turned out it was dead simple. Immediately on CodeProject, I found Davide Pizzolato's article CExif, and in 15 minutes, after modifying his example, I had a working program! The other several hours went into polishing its look and adding features. Wink | ;-)

Usage

To use the tool, simply drop JPG files on the main window and their dates will be automatically updated. The files must have extension JPG or JPEG. If some image files don't have EXIF tag, or the date stored in the EXIF is the same as last modified date, no action will be taken on those files.

I added "Keep Window on Top" option for convenience and this preference is stored in the Windows Registry.

You can also drag and drop folders and the program will process all the files in those folders and subfolders (if this option is checked).

If you suddenly realize that you just made a mistake, there is an Undo button to revert dates on the last batch of files that you dropped. You can also view a detailed log of all processed files.

About the Code

For details on reading EXIF from JPG files with C++, see Davide Pizzolato's article CExif.

Actually, while trying to fix a bug in CExif for ver 1.4, I had to learn a great deal about EXIF structure - more than I ever wanted to. The document that I found most helpful can be found here. Written by TsuruZoh Tachibanaya, it cuts straight to the chase and has lots of easy to follow tables.

To change the date, I simply use MFC's CFile::GetStatus and CFile::SetStatus functions. One thing that is omitted in MSDN documentation about CFile::SetStatus is that it throws an exception if it fails to set a status for any reason. So be sure to wrap SetStatus call with a try/catch block:

C++
try	
{	
	CFile::SetStatus(strName, fs);
}
catch (CException* e)
{
	e->Delete();
	// You can use ::GetLastError() to get Windows error code
}

Another interesting challenge for me was trying to make the project to build in both VC6 and VS2008. The challenging part was the manifest file in resources for XP themes. The project was originally built in VC6, but then I converted it to VS2008. Any modification to resources in VS2008 IDE would result in it writing RT_MANIFEST into .rc file instead of the number 24. If you were then to open such a project in VC6 and build, the visual styles would not be applied because RT_MANIFEST is not defined there and resource compiler treats it as a literal string “RT_MANIFEST”.

The solution was to create a header file with RT_MANIFEST defined in it and add it to resource includes. Now you can edit resource in either IDE and it will still build with visual styles in VC6 and VS2008.

In one of the updates (1.3), I also added message pumping to keep interface responsive when processing thousands of files. Multi-threading is usually a preferred method for this, but I thought it was overkill for a simple program like this.

UPDATE 2012-11-08: In version 1.5 I converted the project to Unicode which was not such a hard task as I initially thought it was. Now the program is able to handle files with international characters in their names  As a result though I had to drop VC6 support since the project would not compile in it. To read and write log and undo file in Unicode I used CStdioFileEx a nice extension class written by David Pritchard.

Links

Here are all the links to other people's work that I used to create this tool:

History

  • 2012-11-08, v1.5
    • Converted project to Unicode to handle international characters in file names.
  • 2010-05-08, v1.4
    • Fixed bug in CExif that would not extract EXIF from certain JPG files 
  • 2010-01-04, v1.3
    • Fixed folder processing bug (Thanks to Bernhard)
    • Fixed initial combo selection bug – it was empty (Bernhard)
    • Fixed EXIF date tag checking (Thanks to sj1172)
    • Added error checking when modifying date. The program will no longer abort with error message.
    • Added status update during lengthy operations. The interface is more responsive now when processing lots of files. (Suggestion by sj1172)
    • Added ability to view log file after processing is complete
    • Changed reference to Hans Dietrich’s article from XColorStatic to XHTMLStatic – didn’t notice this mistake since the original posting
  • 2009-07-23, v1.2
    • Added ability to recognize and process folders and subfolders
    • Added “Process subfolders” checkbox
    • Added Undo functionality to be able to undo last batch
    • Added VS2008 solution to the project source
  • 2008-08-07, v1.1
    • Added combobox that allows user to choose which date to correct: Date Modified, Date Created, or both
  • 2008-08-02, v1.0
    • Initial posting to CodeProject

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to download executable Pin
Manoj Phirke11-Nov-23 1:16
Manoj Phirke11-Nov-23 1:16 
AnswerRe: How to download executable Pin
Damir Valiulin13-Nov-23 5:20
Damir Valiulin13-Nov-23 5:20 
QuestionFlagged by Defender as a trojan... Pin
Member 1179696329-Sep-22 11:45
Member 1179696329-Sep-22 11:45 
AnswerRe: Flagged by Defender as a trojan... Pin
OriginalGriff29-Sep-22 11:50
mveOriginalGriff29-Sep-22 11:50 
GeneralRe: Flagged by Defender as a trojan... Pin
Sean Ewington29-Sep-22 12:10
staffSean Ewington29-Sep-22 12:10 
GeneralRe: Flagged by Defender as a trojan... Pin
Damir Valiulin8-Feb-23 10:18
Damir Valiulin8-Feb-23 10:18 
QuestionHow Do I Drag and Drop Here? Pin
Member 147088946-Jan-20 19:05
Member 147088946-Jan-20 19:05 
QuestionAbsolutely fantastic! Pin
Member 1378361116-Apr-18 23:05
Member 1378361116-Apr-18 23:05 
PraiseExactly what I was searching for! Pin
Member 1375524730-Mar-18 10:01
Member 1375524730-Mar-18 10:01 
GeneralPerfect Pin
Member 118949558-Aug-15 1:29
Member 118949558-Aug-15 1:29 
QuestionAMAZING Pin
Member 115959299-Apr-15 22:00
Member 115959299-Apr-15 22:00 
GeneralMy vote of 5 Pin
Member 1152973717-Mar-15 4:39
Member 1152973717-Mar-15 4:39 
BugThe parameter is incorrect. Pin
Member 1074636513-Apr-14 14:46
Member 1074636513-Apr-14 14:46 
GeneralRe: The parameter is incorrect. Pin
Damir Valiulin14-Apr-14 6:03
Damir Valiulin14-Apr-14 6:03 
GeneralRe: The parameter is incorrect. Pin
DemonCamber X (XDemonCamberX)9-Jul-14 9:08
DemonCamber X (XDemonCamberX)9-Jul-14 9:08 
GeneralRe: The parameter is incorrect. Pin
DemonCamber X (XDemonCamberX)9-Jul-14 9:25
DemonCamber X (XDemonCamberX)9-Jul-14 9:25 
QuestionRemove "jpg" file filter or allow "jps" files Pin
peterjwhite27-Jan-13 18:25
peterjwhite27-Jan-13 18:25 
AnswerRe: Remove "jpg" file filter or allow "jps" files Pin
Damir Valiulin14-Feb-13 4:40
Damir Valiulin14-Feb-13 4:40 
GeneralRe: Remove "jpg" file filter or allow "jps" files Pin
peterjwhite14-Feb-13 13:45
peterjwhite14-Feb-13 13:45 
QuestionThanks! Now it works perfectly even for Unicode file names Pin
Vladimir Dvorak15-Nov-12 21:44
Vladimir Dvorak15-Nov-12 21:44 
GeneralMy vote of 5 Pin
gndnet9-Nov-12 23:46
gndnet9-Nov-12 23:46 
QuestionWould it be possible to extend to Unicode characters? Pin
Vladimir Dvorak30-Oct-12 7:59
Vladimir Dvorak30-Oct-12 7:59 
AnswerRe: Would it be possible to extend to Unicode characters? Pin
Damir Valiulin7-Nov-12 8:01
Damir Valiulin7-Nov-12 8:01 
QuestionThank you! Pin
Member 86217744-Feb-12 19:08
Member 86217744-Feb-12 19:08 
QuestionThanks Pin
jolindien22-Jan-12 1:55
jolindien22-Jan-12 1:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.