In addition to the bits of an image, JPEG files have timestamps and other information embedded inside. The metadata consists of EXIF (Exchangable Image File Format) tags. Click here for EXIF specifications.
You can extract a JPEG file's timestamp as follows:
The following console application uses the above technique to rename all JPEG files in a folder and subfolders to filenames based on the JPEG files' timestamps:
using System; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace RenJPEGToTS { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Usage: RenJPEGToTS { folder }"); } else { string folder = args[0]; Console.WriteLine(string.Format("Renaming files in {0} and subfolders\n", folder)); RenameFiles(folder); } } // Rename JPEG files in the specified folder (and subfolders) to filenames // based on their JPEG timestamps. private static void RenameFiles(string Folder) { Encoding enc = new ASCIIEncoding(); // For each JPEG file in the folder... foreach (string oldFilePath in Directory.GetFiles(Folder, "*.jpg")) { string newFilePath = null; // Create an Image object corresponding to the JPEG file. using (Image img = Bitmap.FromFile(oldFilePath)) { // Extract all PropertyItems from the Image. foreach (PropertyItem pi in img.PropertyItems) { // If the PropertyItem is a timestamp... if (pi.Type == 2 /* text */ && pi.Id == 306 /* date and time */) { // Create a filename based on the timestamp. string newFileName = enc.GetString(pi.Value).Replace(" ", "_").Replace(":", "_"); newFilePath = Path.GetDirectoryName(oldFilePath) + @"\" + newFileName.Substring(0, newFileName.Length - 1) + ".jpg"; } } } if (newFilePath != null && newFilePath != oldFilePath) { Console.WriteLine(string.Format("Renaming {0} to {1}", oldFilePath, newFilePath)); // Rename the file. File.Move(oldFilePath, newFilePath); } } // Rename JPEG files in each subfolder. foreach (string folderPath in Directory.GetDirectories(Folder)) { RenameFiles(folderPath); } } } }
Title | Date |
.NET Public-Key (Asymmetric) Cryptography Demo | July 20, 2025 |
Raspberry Pi 3B+ Photo Frame | June 17, 2025 |
EBTCalc (Android) Version 1.53 is now available | May 19, 2024 |
Vault 3 Security Enhancements | October 24, 2023 |
Vault 3 is now available for Apple OSX M2 Mac Computers! | September 18, 2023 |
Vault (for Desktop) Version 0.77 Released | March 26, 2023 |
EBTCalc (Android) Version 1.44 is now available | October 12, 2021 |