Skip to content

Instantly share code, notes, and snippets.

@dliggat
Created Mar 21, 2014
Embed
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 ArgumentError.new "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 ArgumentError.new "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
FileUtils.mv abs_file, photo_abs_dir
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment