Created
July 19, 2012 10:53
-
-
Save datenimperator/3142998 to your computer and use it in GitHub Desktop.
Given a number of home directories containing Maildir subdirectories, I want to recursively count both unread and total messages, along with the date/time of the first/last message in that Maildir. This script is supposed to run from the base directory co
This file contains 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 -w | |
result = Hash.new{|h, k| h[k] = { | |
:count => 0, | |
:unread => 0, | |
:first => nil, | |
:last => nil | |
} | |
} | |
Dir.glob("**/{cur,new}", File::FNM_DOTMATCH).each do |d| | |
parts = d.split(File::SEPARATOR).reverse | |
username = parts.pop | |
parts.pop # remove 'Maildir' | |
r = result[username] | |
size = Dir.entries(d).size - 2 | |
if parts.first == 'new' | |
r[:unread] += size | |
end | |
r[:count] += size | |
Dir.entries(d).each do |f| | |
next if ['.', '..'].include?(f) | |
fp = File.expand_path(f, d) | |
r[:first] = File.mtime(fp) if r[:first].nil? || File.mtime(fp)<r[:first] | |
r[:last] = File.mtime(fp) if r[:last].nil? || File.mtime(fp)>r[:last] | |
end | |
end | |
puts "email;count;unread;first;last" | |
result.each do |user, values| | |
print "#{user}@domain.com;#{values[:count]};#{values[:unread]};" | |
print "#{values[:first].strftime('%d.%m.%Y %H:%M:%S')}" unless values[:first].nil? | |
print ";" | |
print "#{values[:last].strftime('%d.%m.%Y %H:%M:%S')}" unless values[:last].nil? | |
puts | |
end | |
STDOUT.flush |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment