Created
March 21, 2014 01:18
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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