Skip to content

Instantly share code, notes, and snippets.

@udzura
Created July 13, 2009 00:31
Show Gist options
  • Save udzura/145854 to your computer and use it in GitHub Desktop.
Save udzura/145854 to your computer and use it in GitHub Desktop.
usage: ruby mailcount.rb [searchDir] [outputFile]
#!/usr/bin/ruby
require 'rubygems'
require 'tmail'
re = /^(.*[^\/\*])((?:\/)?(?:\*)?)$/
dir = ARGV[0] ? ARGV[0].gsub(re, "\\1/*{.txt,.eml}") : "./*{.txt,.eml}"
outfile = ARGV[1] ? ARGV[1] :
"./result_#{Time.now.strftime('%y%m%d.%H%M%S')}.csv"
puts "search for: #{dir}", "output into: #{outfile}"
mails = Dir.glob(dir).delete_if{|name| /\.rb$/ =~ name}
count = {}
mails.each do |file|
dt = TMail::Mail.load(file).date.strftime("%Y/%m/%d")
count[dt] ? count[dt] += 1 : count[dt] = 1
end
f = File.open(outfile, "w+")
count.keys.sort.each do |k|
f.puts [k, count[k]].join(',')
end
f.close
puts "successfully end."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment