📜 ⬆️ ⬇️

Change file date according to EXIF

After acquiring a digital soap dish, I needed to catalog the captured files at least by the seasons. But, as it turned out, after copying from the camera, the date of the file change is set to the date of copying and then in order to find out the real time of shooting, you need to look at the EXIF ​​information or attributes of the files. Of course, any self-respecting file viewing program can extract the shooting date from EXIF, but this method is inconvenient for me.
So I decided to write a small C # program that looks for * .jpg files in the current or specified directory, extracts the shooting time and, if this time does not match the file change time, it sets this time according to the shooting date and time. I hope there will be those to whom it is useful.
Yes, maybe there are graphic programs in which similar functionality is built in, but I was also aiming to write something in C #

Thanks to this article for useful information.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Windows.Media.Imaging; namespace ByteProgram.DateToEXIF { class ReadFiles { static void Main(string[] args) { string curDir; curDir = Directory.GetCurrentDirectory(); IEnumerable<string> jpgFilesInDir; if (args.Length > 0) { if(args[0] == "/?") { Console.WriteLine("        "); Console.WriteLine(": "); Console.WriteLine("    -    "); Console.WriteLine(" <> -    "); return; } if (Directory.Exists(args[0])) { jpgFilesInDir = Directory.EnumerateFiles(args[0], "*.jpg"); //      } else { Console.WriteLine("   "); return; } } else { jpgFilesInDir = Directory.EnumerateFiles(curDir, "*.jpg"); //      } FileInfo fInf; foreach (string currentFile in jpgFilesInDir) //    { fInf = new FileInfo(currentFile); Console.WriteLine(" : {0}", fInf.Name); ReadExifInfo rex = new ReadExifInfo(currentFile); try { if (rex.CreateTime.Date.Year > 1980) { Console.WriteLine(" : {0}", rex.CreateTime); if (fInf.LastWriteTime != rex.CreateTime) { Console.WriteLine("    ."); fInf.LastWriteTime = rex.CreateTime; } else { Console.WriteLine(" ,   ."); } } else { Console.WriteLine("  ."); } } catch (Exception e) { Console.WriteLine(e.Message); } } } } class ReadExifInfo { //  EXIF    private DateTime creationTime; private FileStream Foto; private BitmapMetadata TmpImgEXIF; public ReadExifInfo (string fileName) { // try { Foto = File.Open(fileName, FileMode.Open, FileAccess.Read); //     fileName   } catch (Exception) { Console.WriteLine("  "); } try { JpegBitmapDecoder decoder = new JpegBitmapDecoder(Foto, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.Default); //""     decoder TmpImgEXIF = (BitmapMetadata)decoder.Frames[0].Metadata.Clone(); //    creationTime = Convert.ToDateTime(TmpImgEXIF.DateTaken); //    Foto.Close(); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("  EXIF ."); Foto.Close(); } } public DateTime CreateTime { //     get { return creationTime; } } } } 

')

Source: https://habr.com/ru/post/175589/


All Articles