Create a gist now

Instantly share code, notes, and snippets.

@ik5 /maildir.rb
Last active Aug 29, 2015

moving maildir from disorgenized method to an orginized method.
#!/usr/bin/env ruby
require 'fileutils'
SOURCE = '/tmp/mbox/'
DESTINATION = '/tmp/maildir/domain.com/'
def is_maildir(path)
dirs = Dir[File.join(path, '*')]
dirs.select {|name| File.directory?(File.join(path, name)) &&
!(name == '.' or name == '..') }
dirs.include?(File.join(path,'new')) &&
dirs.include?(File.join(path,'Trash')) &&
dirs.include?(File.join(path,'tmp'))
end
def lookup(path)
valid = []
Dir.foreach(path) do |name|
next if name == '.' or name == '..'
fpath = File.join(path,name)
#print "Going over #{fpath} ... "
next unless File.directory?(fpath)
if is_maildir(fpath)
valid.push(fpath)
#puts 'yes'
else
#puts 'no'
valid = valid + lookup(fpath)
end
end
return valid
end
FileUtils::mkdir_p(DESTINATION)
list = lookup(SOURCE)
list.each do |dir|
print "Copying #{dir} to #{DESTINATION} ... "
begin
FileUtils::cp_r(dir, DESTINATION)
rescue => e
puts "Error: #{e.message}"
else
puts 'done'
end
end
@ynonp
ynonp commented Aug 9, 2014

הי,
קצת ארוך... לא יותר פשוט:

find . -type d -exec bash -c "[[ -d {}/new && -d {}/Trash && -d {}/tmp ]] && echo {}" ;

@ik5
Owner
ik5 commented Aug 11, 2014

Indeed a much better solution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment