Skip to content

Instantly share code, notes, and snippets.

@daniel-nelson
Created May 19, 2018 23:13
Show Gist options
  • Save daniel-nelson/887f7d43be43499c03d683ba86860771 to your computer and use it in GitHub Desktop.
Save daniel-nelson/887f7d43be43499c03d683ba86860771 to your computer and use it in GitHub Desktop.
require 'fileutils'
type_map = {
jpeg: 'Photos',
JPEG: 'Photos',
jpg: 'Photos',
JPG: 'Photos',
png: 'PNGs',
PNG: 'PNGs',
mov: 'Videos',
MOV: 'Videos',
mp4: 'Videos',
MP4: 'Videos',
avi: 'Videos',
AVI: 'Videos',
m4v: 'Videos',
M4V: 'Videos',
psd: 'Photoshop',
PSD: 'Photoshop',
}
type_map.each do |extension, root_dir|
Dir.glob("./**/*.#{extension}").each do |filepath|
filename = File.basename(filepath)
created_at = File.birthtime(filepath)
year = created_at.year
month = created_at.month.to_s.rjust(2, '0')
dirpath = "#{root_dir}/#{year}"
Dir.mkdir(dirpath) unless Dir.exists?(dirpath)
dirpath = "#{root_dir}/#{year}/#{created_at.strftime('%Y-%m %b')}"
Dir.mkdir(dirpath) unless Dir.exists?(dirpath)
new_filepath = "./#{dirpath}/#{filename}"
puts new_filepath
FileUtils.mv(filepath, new_filepath) unless new_filepath == filepath
end
end
Dir['**/'].reverse_each { |d| Dir.rmdir(d) if Dir.entries(d).size == 2 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment