saveuo.rb
#----------------------------------------------------------
# saveuo.rb -  Save all user objects of a given type to a
#	       directory. Create dir if it does not exist
#	       Use the preferred name for saving. 
#	       If duplicate name (append a digit)	
#
#  usage: saveuo <capture-filename> <uo-type> <output-dir>
#
#---------------------------------------------------------
require 'win32ole'
 
USAGE = "saveuo <capture-filename> <uo-type> <output-dir>"
 
if ARGV.length != 3
	puts USAGE
	exit 1
end
 
InFile = ARGV[0]
UOType = ARGV[1]
OutDir = ARGV[2]
 
 
UnsniffDB = WIN32OLE.new("Unsniff.Database")
UnsniffDB.Open(InFile)
UOIndex = UnsniffDB.UserObjectsIndex
 
# ensure valid output directory
if !File.directory?(OutDir)
	print "Creating output directory #{OutDir}\n"
	Dir.mkdir(OutDir)
end
 
uniqcnt = 0
(0..UOIndex.Count-1).each  do |idx|
	uo = UOIndex.Item(idx)
	if ! uo.HasError and uo.Type == UOType
		prefname = uo.PreferredFileName
		if prefname.length == 0 
			prefname = "Unnamed-user-object-#{uniqcnt}.uo"
			uniqcnt += 1
		end
		fullpath = File.join(OutDir,prefname)
		if File.exists?(fullpath)
			prefname = "#{uniqcnt}_#{uo.PreferredFileName}"
			fullpath = File.join(OutDir,prefname)
			uniqcnt += 1
		end
 
		fullpath.sub!(/\//,'\\')
		print "Saving to file #{fullpath}..."
		uo.SaveToFile(fullpath)
		print "done\n"
 
	end
 
end
 
 
UnsniffDB.Close