Hi all,
I have way too many pictures on my PC and they are everywhere. I am sure many of you have same problem.
I am attempting to create a simple program to organize them a bit and minimizing manual work, but unfortunately there will still be some.
Please take a look through this code and let me know if you have any suggestions. I am not very happy with how different pictures with same name is handled. How can I improve this code?
Thanks!
//Start Execution here...
private void startBtn_Click(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo("C:\\srcPictures");
WalkDirectoryTree(di);
}
private void WalkDirectoryTree(System.IO.DirectoryInfo root)
{
FileInfo[] files = null;
DirectoryInfo[] subDirs = null;
// First process files under root folder
files = root.GetFiles("*.*");
Logger("Processing Files under: " + root.FullName);
if (files != null)
{
foreach (FileInfo fi in files)
{
string picTakenOn = getDateTaken(fi, false);
string targetDir = "";
if (picTakenOn.Contains("DATETAKEN"))
{
picTakenOn = picTakenOn.Replace("DATETAKEN", "");
targetDir = "C:\\organizedPictures\\" + picTakenOn + "\\";
if (!Directory.Exists(targetDir))
{
Directory.CreateDirectory(targetDir);
}
copyFileToDestination(fi, fi.FullName, targetDir + fi.Name);
}
else if (picTakenOn.Contains("DATEMODIFIED"))
{
picTakenOn = picTakenOn.Replace("DATEMODIFIED", "");
targetDir = destDirectory.Text + "\\Sorted_DateModified\\" + picTakenOn + "\\";
if (!Directory.Exists(targetDir))
{
Directory.CreateDirectory(targetDir);
}
Logger("DATEMODIFIED used instead of DATETAKEN for: " + fi.FullName);
copyFileToDestination(fi, fi.FullName, targetDir + fi.Name);
}
else if (picTakenOn.Contains("INVALID"))
{
Logger("Not an image (Skipped): " + fi.FullName);
}
}
// Resursion for all subdirectories
subDirs = root.GetDirectories();
foreach (System.IO.DirectoryInfo dirInfo in subDirs)
{
WalkDirectoryTree(dirInfo);
}
}
}
private void copyFileToDestination(FileInfo srcInfo, string sourcePath, string destPath)
{
if (File.Exists(destPath)) //Same file name already exists
{
FileInfo destInfo = new FileInfo(destPath);
if (srcInfo.Length != destInfo.Length) //If different images (check by filesize)
{
Logger("FILENAME conflict for: " + sourcePath);
copyFileToDestination(srcInfo, sourcePath, destPath + "_dup" + srcInfo.Extension);
}
else
{
Logger("SKIPPED copy (Dup found) for: " + sourcePath);
}
}
else
{
File.Copy(sourcePath, destPath);
}
}
private string getDateTaken(FileInfo fi, bool includeDate)
{
if (fi.Extension != ".jpeg" && fi.Extension != ".jpg" && fi.Extension != ".JPEG" && fi.Extension != ".JPG")
return "INVALID";
Regex r = new Regex(":");
FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
Image myImage = Image.FromStream(fs, false, false);
try
{
PropertyItem propItem = myImage.GetPropertyItem(36867); //36867 is for Date taken
string dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2).Split(' ')[0];
return ("DATETAKEN" + dateTaken.Split('-')[0] + "-" + dateTaken.Split('-')[1]);
}
catch
{
string dateModified = fi.LastWriteTime.ToString();
return ("DATEMODIFIED" + dateModified.Split(' ')[0].Split('/')[2] + "-" + dateModified.Split(' ')[0].Split('/')[1]);
}
}
public void Logger(String lines)
{
//Set the log file
string logFile = "C:\\organizedPictures\\log.txt";
System.IO.StreamWriter file = new System.IO.StreamWriter(logFile, true);
file.WriteLine(lines);
file.Close();
}