– go to postRubyでNotesのメールを書き出す
を参考に、Notes添付ファイルを一括出力する処理をRubyで書いてみました。
まんまコピーなんですが、忘れないように記録
全件対象で処理した場合、エラーが発生し落ちてしまったので、
@Attachmentsで抽出したViewを作成し処理対象を添付ファイルが
存在する文書のみに変更しました。
require 'win32ole'
class Log
def initialize(s)
@@logger ||= Proc.new { |x| puts x }
@@logger.call(s)
end
def Log.register_logger(p)
@@logger = p
end
end
class Notes
class Session
def initialize(server, dbname)
@server, @dbname = server, dbname
@session = WIN32OLE.new('Notes.NotesSession') or return nil
@db = @session.GetDatabase(@Server, @dbname)
end
def each
view = @db.GetView('Attachments')
doc = view.GetFirstDocument
while doc
yield Notes::Document.new(doc)
doc = view.GetNextDocument(doc)
end
end
include Enumerable
#添付ファイルをdirに書き出す
def extract_files(dir)
self.each do |doc|
doc.each_attachment do |obj|
name = obj.name
Log.new "\t#{name}"
#対象外の拡張子をチェックする
ext = File.extname(name).downcase
next if %w(htm html csv pif scr com bat).include?(ext)
#書き出しメソッドを呼び出し
doc.extract_file(name, "#{dir}/#{name}")
end
end
end
#添付ファイルをdir/fromに書き出す
def extract_files_by_sender(dir, body_hash = nil)
self.each do |doc|
k = doc.unid
#出力先の作成
extract_dir = "#{dir}/#{k}"
Dir.mkdir(extract_dir) unless File.exist?(extract_dir)
doc.each_attachment do |obj|
name = obj.name
Log.new "\t#{name}"
ext = File.extname(name).downcase
next if %w(htm html csv pif com bat).include?(ext)
doc.extract_file(name, "#{extract_dir}/#{name}")
end
end
end
end
class Document
def initialize(doc)
@doc = doc
end
def each_attachment
a = @doc.Items or return
a.each do |obj|
yield Notes::Attachment.new(obj) if obj.Type == 1084 #ATTACHMENT
end
end
def unid; @doc.UniversalID; end
def extract_file(name, path)
obj = @doc.GetAttachment(name) or return
begin
obj.ExtractFile(path)
rescue => e
Log.new "**** #{e.message} ****"
end
end
end
class Attachment
def initialize(obj)
@obj = obj
end
def extract_file(path)
@obj.ExtractFile(path)
end
def name
@obj.Values[0]
end
end
end
呼出し側
require 'notesruby'
require 'optparse'
opt_hash = Hash.new
opt_hash['dir'] = '.'
p ARGV
ARGV.options do |opt|
opt.on('-d [dir]') { |v| opt_hash['dir'] = v }
opt.on('-s') { |v| opt_hash['same_folder'] = v }
opt.parse!
end
dir = File.expand_path(opt_hash['dir'])
if !File.writable?(dir) || ARGV.size == 0
Log.new "Usage: ruby #{$0} [-s] [-d dir] file [file...]"
exit
end
body_hash = Hash.new
ARGV.each do |arg|
if m = arg.match(/(.*)@(.*)/)
nsf, svr = m[1, 2]
else
nsf, svr = [arg, '']
end
if db = Notes::Session.new(svr, nsf)
Log.new "添付ファイル抽出...(#{arg})"
if opt_hash['same_folder']
db.extract_files(dir)
else
db.extract_files_by_sender(dir, body_hash)
end
end
end