Skip to content

Instantly share code, notes, and snippets.

Created Mar 21, 2014
What would you like to do?
A ruby script to bin photos by year-month directories (assumes jpgs are in a standard ISO8601ish format; e.g. 2014-01-10_13-23-22.jpg).
#!/usr/bin/env ruby
require 'date'
require 'fileutils'
raise "Usage: #{$0} directory" unless ARGV.count == 1
root_dir = File.expand_path ARGV.first
Dir.glob "#{root_dir}/*.jpg" do |file|
match = /.*?(\d\d\d\d)-(\d\d)-\d\d_\d\d-\d\d-\d\d.*\.jpg$/.match file
raise "File did not match: #{file}" unless match
year = match[1]
month = match[2]
month_abbr = Date::MONTHNAMES[month.to_i][0..2]
photo_dir_name = "#{year}-#{month}-#{month_abbr}"
photo_abs_dir = File.join root_dir, photo_dir_name
abs_file = File.expand_path file
Dir.mkdir photo_abs_dir unless Dir.exist? photo_abs_dir abs_file, photo_abs_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment