Skip to content

Instantly share code, notes, and snippets.

@jonsagara
Created October 13, 2013 23:26
Show Gist options
  • Save jonsagara/6968496 to your computer and use it in GitHub Desktop.
Save jonsagara/6968496 to your computer and use it in GitHub Desktop.
Copy a flat directory of iPhone pictures and videos into a YYYYMM folder structure, based on the date the picture/video was taken.
class Program
{
private const string SourceDirectory = @"D:\iPhoneMedia";
private const string DestDirectory = @"D:\iPhoneMediaSorted";
static void Main(string[] args)
{
var sourceDir = new DirectoryInfo(SourceDirectory);
// Group by YYYYMM
var yearMonth = sourceDir
.GetFiles()
.OrderBy(fi => fi.LastWriteTime)
.GroupBy(fi => (fi.LastWriteTime.Year * 100) + fi.LastWriteTime.Month)
.ToList();
foreach (var ym in yearMonth)
{
Console.WriteLine("{0}: {1}", ym.Key, ym.Count());
var destFolder = Path.Combine(DestDirectory, ym.Key.ToString());
if (!Directory.Exists(destFolder))
{
Directory.CreateDirectory(destFolder);
}
foreach (var fi in ym)
{
var destFile = Path.Combine(destFolder, fi.Name);
Console.WriteLine("Copying {0} to {1}", fi.Name, destFile);
fi.CopyTo(destFile, overwrite: true);
}
}
Console.WriteLine("Press ENTER to quit...");
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment