Skip to content

Instantly share code, notes, and snippets.

@dliggat
Created March 21, 2014 01:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dliggat/9677631 to your computer and use it in GitHub Desktop.
Save dliggat/9677631 to your computer and use it in GitHub Desktop.
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