Sunday, April 25, 2010

Coding For Fun: Categorizing Photos by Date Taken

The Problem

I was trying to organize photos of my cats on my hard drive today, they all sit in this one folder called "Cats", unsorted and with no hierarchy. Since I wanted to keep track of my cats' growth progress and sort photos according to the different periods, logically, I decided to place them into different subfolders according to which year and month they were taken. Sounds like an excellent solution, doesn't it?

The Solution
  1. Look at photo
  2. Check the Date Taken field of the photo
  3. Create folder with the photo's taken year and month in format "YYYY.MM"
  4. Move the photo into the corresponding folder
  5. Repeat all steps above for the next photo
Here's what I did manually for about 20-30 photos... until I realized I have over 2000 photos waiting for me to go through. So I was thinking, there must be a better and faster way to do this. In other words, guess it's time for the nerdy side of me to kick in ;)

The Better Solution

Yes, let's build an app to automate it! I went with .NET 4 since I just installed it along with Visual Studio 2010 a few days ago (and I love to try new things out!) and built a simple interface using XAML in WPF:

It's very quick and easy to build an interface like this using XAML in WPF, especially if you're familiar with XML. For the interface I have above, I simply started off by defining the grid columns and grid rows using Grid.RowDefinitions and Grid.ColumnDefinitions and continued by placing the UI components into the grid. In particular, I defined 4 rows and 2 columns for the interface above:
  • TextBlock on Row 0, Column 1 for program description (Note: Grid rows/columns in WPF begins with 0)
  • "Directory" Label on Row 1, Column 0
  • TextBox for retrieving target directory on Row 1, Column 1
  • Two buttons - Categorize and Reset - on Row 2, Column 1
  • Copyright TextBlock on Row 3, Column 1
For example, to place the label "Directory" on Row 1, Column 0 of the grid, all you need is to specify the Grid.Row and Grid.Column position in the XAML for the label:

  1. <Label Grid.Row="1" Grid.Column="0" x:Name="labelDir" VerticalAlignment="Center" Padding="2" Margin="2">Directory: </Label>

After creating the interface, I proceeded with the backend work. The basic logic of the program is very similar to the manual solution we listed above, i.e. loop through all the photos in the target directory, check their Date Taken value from the metadata, and place them in their corresponding directory (create one if one does not exist). Now the harder question is, how do we read the metadata and retrieve the Date Taken value using C# in .NET 4?

Fortunately, since .NET 3, we've been able to retrieve metadata from basic photos using System.Windows.Media.Imaging.BitmapSource via image piping. For WPF, supported formats include BMP, GIF, JPEG, PNG, and TIFF images. Once we have created the BitmapSource object for our image, the BitmapSource.Metadata property can return all metadata associated with the image. Therefore, we'd be able to retrieve the Date Taken value of the photo as required:
Finally, we just need to parse in the Date Taken String (it's returned as a String from the BitmapSource.Metadata property), create a directory if needed, and move the file to their corresponding subfolder. These steps could be achieved using System.IO.Directory and System.IO.File.

Final Results

The program was able to go through all supported format photos from a specified directory and move the ones with an existing Date Taken value into their corresponding folder (in "YYYY.MM" format for the ease of sorting)! In particular, the program automatically went through over 2000 of my cat photos and moved all 1265 valid photos into the correct subfolders (took approximately 5 minutes with no special optimization). What's better is that I can now re-use this tool for similar scenarios in the future!

Successfully moved 1265 photos to their corresponding subfolders.

Successfully moved 1265 photos to their corresponding subfolders.Subfolders are created in the target directory when needed.

Photos are moved into their corresponding subfolders. These photos are taken in June, 2006.

...and this is one reason why I love technology and why I like to code for fun! I hope this post helped those of you who are interested in creating tools alike. If you're interested in getting a copy of the tool, feel free to contact me in the comments or email me at

For those of you who are NOT geeky...

If you use Windows Live Photo Gallery and have included your photos in the library, you can easily sort or filter the photos by Date Taken with just a few clicks: In the navigation pane, under Date taken, click a year to see all of the photos and videos taken during that year, and then select a month or date if required.

Happy coding! Meow!

Wednesday, November 4, 2009

Migrating Windows 7 Sticky Notes

Very similar to the previous post, simply copy and paste this file from computer A to computer B:
  • C:\Users\Vivien\AppData\Roaming\Microsoft\Sticky Notes\StickyNotes.snt
You're done!

Friday, October 23, 2009

How to migrate Windows Live Messenger emoticons

Windows Live Messenger (latest version at the time of this post was Build 14.0.8089.726) does not come with an export/import function for migrating emoticons from one PC to another. However, this can be done simply by copy and pasting the hidden emoticon folder.

Emoticons are stored in the AppData folder under your Windows account. For example, if you are on Windows Vista/7, given that your account name on Windows is "Vivien" and that your Windows Live ID is "", the emoticons should live in this folder:

  • C:\Users\Vivien\AppData\Local\Microsoft\Messenger\\ObjectStore\CustomEmoticons

Now copy this folder from Machine A and paste it into the same named folder and path on Machine B. Restart Windows Live Messenger on Machine B, and you should now see all your emoticons on Machine B!

Note that the AppData folder is hidden by default. The simplest way to get to the folder is by typing in the path directly to your Windows start menu search pane. If you prefer browsing to the folder via Windows Explorer and you're having trouble finding the AppData folder, make sure to show all hidden folders.