Skip to content

Instantly share code, notes, and snippets.

@zstumgoren
Created December 22, 2014 18:47
Show Gist options
  • Select an option

  • Save zstumgoren/a1566e2108e8f2d08b95 to your computer and use it in GitHub Desktop.

Select an option

Save zstumgoren/a1566e2108e8f2d08b95 to your computer and use it in GitHub Desktop.
Quick/dirty script to organize files by year/month.
#!/usr/bin/env ruby
# Quick-and-dirty script that moves HTML files produced by Thunderbird
# into year/month directories;
#TODO: Update script to be more memory-efficient
require 'fileutils'
# COMMENTING THIS OUT. WE NEED TO UPDATE IMPLEMENTATION
# TO ONLY CREATE FOLDERS FOR MONTHS WHERE EMAIL EXISTS#years = (2010..2014).to_a.collect { |year| year.to_s }
#months = (1..12).to_a.collect { |num| num.to_s.rjust(2,'0') }#years.each do |year|
# months.each do |month|
# `mkdir -p Inbox/#{year}/#{month}`
# end
#end
# glob all *.html files outside of year/month dirs
def files_to_move
all_files = Dir.glob("Inbox/**/*.html").reject do |pth|
encoded_path = pth.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
encoded_path.match %r{\d{4}/\d{2}/\d{4}}
end
by_yearmonth = {}
all_files.each do |pth|
base_fname = File.basename(pth)
year, month = base_fname[0..3], base_fname[4..5]
key = [year, month]
begin
by_yearmonth[key] << pth
rescue NoMethodError
by_yearmonth[key] = [pth]
end
end
by_yearmonth
end
# Move html files to year/month dirs
files_to_move.each_pair do |key,files|
year, month = key
target = "Inbox/#{year}/#{month}/"
files.each do |file|
FileUtils.mv file, target
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment