Ruby Script to Create an Empty Copy of Thunderbird's Folder Structure

Posted by Jonathan Altman Tue, 03 Jan 2006 04:01:00 GMT

I like to keep my work email sorted by year to keep the size of the various boxes small for perusal (oh for the day Google sells corporate gmail!). This script is part of a process to accomplish that. It mirrors your existing Thunderbird folder/mailbox structure to a different directory by creating the same directory hierarchy, creating empty mailbox folders, copying your filter rules and popstate files, and skipping the mailbox summary files.

This script may be generally useful for other purposes as well.

#!/usr/bin/env ruby

require 'find'
require 'ftools'

scriptname = File.basename(__FILE__)
unless ARGV.length == 2
    $stderr.puts "Usage: #{scriptname} source_dir dest_dir" 
    exit 1
end
from_dir = ARGV[0]
to_dir = ARGV[1]
sub_start = from_dir.length

Find.find(from_dir) {|f|
    action = "" 

    if (/\.dat$/ =~ f )
        action = "copy" 
    elsif (/\.msf$/ !~ f )
        File::directory?(f) ? action = "mkdir" : action = "touch" 
    end

    if (action != "")
        new_item = to_dir + f[sub_start..-1]
        $stderr.print "#{action} #{new_item}..." 

        case action
        when "copy" 
            File::copy f, new_item, true
        when "mkdir" 
            File::makedirs new_item, true
        when "touch" 
            File::open(new_item, File::CREAT|File::TRUNC|File::RDWR, File::stat(f).mode) {|file|
                $stderr.puts "#{new_item} created" 
            }
        end
    else
        puts "skipping #{f}" 
    end
}

Once you create this new tree, you can copy the insides of each account’s directory into a subfolder that you manually create inside the new tree.

As always, this recipe comes with NO WARRANTY. Always back up your data first. Etc., etc.

Tags , ,  | no comments | 280 trackbacks