ICI logo


Intersystem Concepts, Inc.



VB6 To Get .jpg Exif and Set File Date

You have a batch of .jpg photos from a digital camera, but somewhere along the way the file dates got changed and no longer match when the photo was taken. This can be fixed via program without any Windows or VB add-ons!

Most digicams store the photo date and time in plain text form within the .jpg file. It is something called Exif metadata. Once you know it's in text form, finding it is fairly easy.

All the camera models I have tested store the date in the format yyyy:mm:dd hh:mm:ss within the first 512 characters of the .jpg file. If the photo was taken during the 21st century, the year will be 20xx, so you can simply search for "20" within the beginning of the photo file. This code demonstrates:

	s$ = Space$(512)
	Open "photo.jpg" For Binary As #1
	Get #1, 1, s$
	Close #1
	lpt& = InStr(s$, "20")

Note that it is possible that "20" appears in the first 512 characters for other reasons, so to be precise, after you find a "20" you should examine the characters that follow to check that they resemble a date and time in the form yyyy:mm:dd hh:mm:ss. Something like 2024:12:31 23:59:59 is one second before the next year.

After you find where the year starts, use string parsing to isolate the date and time from this Exif metadata.

If you want to correct the file's date to match that found within the photo's Exif metadata, you can do that, too. VB6 offers no specific function to change a file's date, but you can kludge it by temporarily resetting the computer system's date, as illustrated below:

	sdatefix$ = 		' put your Exif date here
	stimefix$ = 	        ' put your Exif time here
	sdatewas$ = Date$	' save current system date and time
	stimewas$ = Time$
	s1$ = " "		' initialize 1-character buffer
	Date = sdatefix$        ' fake out current date/time
	Time = stimefix$
	Open "photo.jpg" For Binary As #1
	Get #1, 1, s1$          ' read a character
	Put #1, 1, s1$          ' write same to update file's date and time
	Close #1
	Date = sdatewas$        ' restore system date and time
	Time = stimewas$
Hope this helps. I devised this after being unable to find any solution online that did not involve add-ons.
Other VB tips