I am BARRY HESS > Blog

Automatically Pull Photos Off Your Flash Card With Ruby

For several years now, I’ve been using a VBA script on my Windows PC to pull photo files off my compact flash card, rename them, place them into handy folders, and copy them out to my photo storage area. When I switched to Mac, I knew the VBA script was going to be short lived. This weekend I finally laid it to rest.

The result I was looking for with this script is that all photo files on the compact flash card end up in a destination directory, placed in folders named YYYY-MM-DD. The files themselves would be named YYYY-MM-DD(hh.mm.ss). Some may prefer a deeper folder structure, like YYYY/MM/DD for instance. My script could be easily updated to do such a thing.

Since I’m now a Ruby on Rails coder, I figured this would be a great time to learn about using Ruby for scripting. I’ve always heard that Ruby is great for such a purposes, and it turns out that statement is very true.

Before I added the handy option parser to the Ruby script, there were only 18 lines to do what 61 lines of (poorly written) VBA code used to do. Actually, the Ruby script did even more than the original script would do. The original script could only copy from a single directory at a time. The Ruby script copies all photo files from all subdirectories. Basic usage is:

ruby copypics.rb ./source_directory/ ./destination_directory/

The way the script is written, those trailing ‘/’ characters are required. To ease the chance of typos, I suggest an alias (in OS X, add an alias to ~/.bash_profile). I have aliases for grabbing pictures from my card reader as well as The Kid’s kidd-o Fisher Price digital camera:

alias pics="ruby ~/code/copypics.rb /Volumes/Card_Reader/ photos_directory/"
alias picskid="ruby ~/code/copypics.rb /Volumes/Kid_Camera/ kid_photos_directory/"

Note: You’ll need to be explicit about the script location, source, and destination directories when defining your alias.

Using an excellent options parser, found at about.com of all places, the script is able to support the -v (–verbose) and -h (–help) flags. Verbose mode will print out the source and destination of all copied pictures. You would call the script with the verbose flag like so:

ruby copypics.rb -v ./source_directory/ ./destination_directory/

Source code:

copypics.rb getpics.vbs (shudder)