Use C# to organise photos into date folders

23 Apr

I like to keep photos of my children in folders which are named based on the date the photos inside were taken. For example, all photos taken on Christmas Day last year are in a folder called 2010-12-25.

I finally got round to writing a quick C# console application to organise photos into folders automatically. The date the picture was taken is easy to get using the BitmapMetadata class.

Here’s the code:

using System;
using System.IO;
using System.Windows.Media.Imaging;

namespace OrganisePhotosIntoDateFolders
{
class Program
{
static void Main(string[] args)
{
string folderPath = "C:\\PhotosToOrganise";
if (args.Length > 0) folderPath = args[0];
foreach (string photoPath in Directory.EnumerateFiles(folderPath, "*.jpg"))
{
string subFolderName = "Unknown";
string photoFileName = new FileInfo(photoPath).Name;
DateTime dateTaken = DateTime.MinValue;
using (FileStream fs = new FileStream(photoPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
BitmapMetadata md = (BitmapMetadata)BitmapFrame.Create(fs).Metadata;
DateTime.TryParse(md.DateTaken, out dateTaken);
}
if (dateTaken > DateTime.MinValue) subFolderName = dateTaken.ToString("yyyy-MM-dd");
string subFolderPath = String.Format("{0}\\{1}", folderPath, subFolderName);
if (!Directory.Exists(subFolderPath)) Directory.CreateDirectory(subFolderPath);
File.Move(photoPath, String.Format("{0}\\{1}", subFolderPath, photoFileName));
}
}
}
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.