Commits

Anonymous committed 0789cb8

Use Original date and allow null argument

Comments (0)

Files changed (1)

-using System;
-using System.IO;
-using System.Collections.Generic;
-using com.drew.metadata.jpeg;
-using com.drew.metadata;
-using com.drew.metadata.exif;
-
-
-namespace Renamer
-{
-	class MainClass
-	{
-		public static void Main (string[] args)
-		{
-			String eventName = args[0];
-			
-			String currentDir = Directory.GetCurrentDirectory();
-			
-		
-			List<FileWithInfo> files = new List<FileWithInfo>();
-			
-			String[] filenames = Directory.GetFiles(currentDir, "*.JPG");
-			foreach(String filename in filenames)
-				files.Add(new FileWithInfo(filename, eventName));
-			
-			files.Sort();
-			
-			int position = 1;
-			
-			foreach(FileWithInfo file in files){
-				file.setPosition(position++);
+using System;
+using System.IO;
+using System.Collections.Generic;
+using com.drew.metadata.jpeg;
+using com.drew.metadata;
+using com.drew.metadata.exif;
+
+
+namespace Renamer 
+{
+	class MainClass
+	{
+		public static void Main (string[] args)
+		{
+			String eventName;
+			if (args.GetLength(0)== 0) {
+				eventName = null;
 			}
-			
-			foreach(FileWithInfo file in files){
-				file.doRename();
-			}
-					
-		}
-	}
-	
-	class FileWithInfo : IComparable<FileWithInfo>
-	{
-		String file;
-		String eventName;
-		int position;
-		
-		public FileWithInfo(String file, String eventName){
-			this.file = file;
-			this.eventName = eventName;
-		}
-		
-		public void setPosition(int position) {
-			this.position = position;
-		}
-		
-		public void doRename(){
-			string newName = new FileInfo(file).Directory.FullName + Path.DirectorySeparatorChar + eventName + "_" + position + ".jpg";
-			Console.WriteLine(file + " -> " + newName);
-			File.Move(file, newName);
-		}
-		
-		DateTime getDateTaken() {
-			ExifReader reader = new ExifReader(new FileInfo(file));
-			Metadata meta = reader.Extract();
-			AbstractDirectory tags = meta.GetDirectory(typeof(ExifDirectory).FullName);
-			return tags.GetDate(ExifDirectory.TAG_DATETIME);
-		}
-		
-		public int CompareTo(FileWithInfo other) {
-			return getDateTaken().CompareTo(other.getDateTaken());
-		}
-	}
-}
-
+			else {
+				eventName = args[0];
+			}
+			
+			String currentDir = Directory.GetCurrentDirectory();
+					
+			List<FileWithInfo> files = new List<FileWithInfo>();
+			
+			String[] filenames = Directory.GetFiles(currentDir, "*.JPG");
+			foreach (String filename in filenames)
+				files.Add(new FileWithInfo(filename, eventName));
+			
+			files.Sort();
+			
+			int position = 1;		
+			foreach (FileWithInfo file in files) {
+				file.setPosition(position++);
+				file.doRename();
+			}
+					
+		}
+	}
+	
+	class FileWithInfo : IComparable<FileWithInfo>	{
+		String file;
+		String eventName;
+		String dateTime;
+		int position;
+		
+		public FileWithInfo(String file, String eventName) {
+			this.file = file;
+			this.eventName = eventName;
+			ExifReader reader = new ExifReader(new FileInfo(file));
+			Metadata meta = reader.Extract();
+			AbstractDirectory tags = meta.GetDirectory(typeof(ExifDirectory).FullName);
+			this.dateTime = tags.GetString(ExifDirectory.TAG_DATETIME_ORIGINAL);
+			if (this.dateTime == null) {
+				this.dateTime = tags.GetString(ExifDirectory.TAG_DATETIME);	
+				if (this.dateTime == null) {
+					this.dateTime = "2000:01:01 00:00:00";
+				}
+			}
+		}
+		
+		public void setPosition(int position) {
+			this.position = position;
+		}
+		
+		public void doRename() {
+			String newName;
+			
+			if (eventName == null) {
+				newName = new FileInfo(file).Directory.FullName + Path.DirectorySeparatorChar + dateTime.Replace(":","-") + "(" + position + ").jpg";
+			}
+			else {
+				newName = new FileInfo(file).Directory.FullName + Path.DirectorySeparatorChar + eventName + "_" + position + ".jpg";
+			}
+			if (!(file.ToLower().Equals(newName.ToLower()))) {
+				Console.WriteLine(file + " -> " + newName);
+				File.Move(file, newName);
+			}
+		}
+		
+		String getDateTaken() {
+			return this.dateTime;
+		}
+		
+		public int CompareTo(FileWithInfo other) {
+			return getDateTaken().CompareTo(other.getDateTaken());
+		}
+	}
+}
+