Browse Source

update, it can find date by file name when get date from EXIF fails

Shenjian 7 years ago
parent
commit
4070c08d75
1 changed files with 40 additions and 12 deletions
  1. 40 12
      PhotoSorter/Program.cs

+ 40 - 12
PhotoSorter/Program.cs

@@ -15,7 +15,7 @@ namespace PhotoSorter
         {
             string postfix = "Hansi";
 
-            Console.WriteLine("This application can sort photos in CURRENT directory by exif shotdate, move the photos to sub-Directory which created by date. Hansi 2017-10-01");
+            Console.WriteLine("This application can sort photos in CURRENT and sub directory by EXIF shotdate Or it's Filename, move the photos to a NEW sub-Directory which created by date like \"2017-10-01\". Hansi 2017-10-21");
             Console.WriteLine("Press any key to DO it...");
             Console.ReadKey();
             string folderIn = AppDomain.CurrentDomain.BaseDirectory;
@@ -28,20 +28,27 @@ namespace PhotoSorter
             foreach (var file in files)
             {
                 k++;
-                Console.WriteLine(k.ToString() + "/" + files.Length.ToString());
-
                 FileInfo fi = new FileInfo(file);
-
+                Console.WriteLine(k.ToString() + "/" + files.Length.ToString() + "\t" + fi.Name);
                 string newFileName;
                 if ((fi.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                 {
                     var dateTaken = GetDateTakenFromImage(file);
                     if (dateTaken == DateTime.MinValue)
                     {
-                        continue;
+                        Console.WriteLine("ignore " + fi.Name + ":Get Exif Fail");
+                        Console.WriteLine("Trying to get Date From filename...");
+                        dateTaken = GetDateFromImageFileName(file);
+                        if (dateTaken == DateTime.MinValue)
+                        {
+                            Console.WriteLine("ignore " + fi.Name + ":Get Date By File Name Fail");
+                            continue;
+                        }
                     }
+
                     if (Path.GetExtension(file).ToLower() != ".jpg")
                     {
+                        Console.WriteLine("ignore " + fi.Name + "NOT jpg File");
                         continue;
                     }
                     //dateTaken = dateTaken.AddHours(hourOffset);
@@ -89,20 +96,41 @@ namespace PhotoSorter
         /// <returns></returns>
         public static DateTime GetDateTakenFromImage(string path)
         {
-            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
-            using (Image myImage = Image.FromStream(fs, false, false))
+            try
             {
-                try
+                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
+                using (Image myImage = Image.FromStream(fs, false, false))
                 {
                     PropertyItem propItem = myImage.GetPropertyItem(36867);
                     string dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
                     return DateTime.Parse(dateTaken);
                 }
-                catch (Exception ex)
-                {
-                    return DateTime.MinValue;
-                }
             }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+                return DateTime.MinValue;
+            }
+        }
+        public static DateTime GetDateFromImageFileName(string path)
+        {
+            //not finished try to get name from filename. yyyyMMdd or yyyy-MM-dd
+            string fn = Path.GetFileNameWithoutExtension(path);
+            Regex r1 = new Regex(@"20\d{6}");
+            Regex r2 = new Regex(@"20\d{2}-\d{2}-\d{2}");
+            if (r1.IsMatch(fn))
+            {
+                string ff = r1.Match(fn).Value;
+                return DateTime.Parse(ff.Substring(0,4)+"/" + ff.Substring(4,2) + "/" + ff.Substring(6,2));
+            }
+            if (r2.IsMatch(fn))
+            {
+                string ff = r2.Match(fn).Value;
+                return DateTime.Parse(ff);
+            }
+
+            return DateTime.MinValue;
+
         }