Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'digest/md5'
- require 'find'
- def sel
- print "*************************************************************************\n"
- end
- module MD5Fixer
- def self.make_header
- print sel
- print "**MD5Fixer v 1.00\n"
- print "**Narzew\n"
- print "**v 1.00\n"
- print "**21.12.2013\n"
- print "**All rights reserved.\n"
- print sel
- print "**Use at your own risk!\n"
- print "**Make backup of data you use with this tool\n"
- print sel
- end
- def self.make_md5(x)
- return lambda{File.open(x,'rb'){|f|return Digest::MD5.hexdigest(f.read)}}.call
- rescue
- print "#{x} MD5 checksum failed\n"
- end
- def self.make_number(nr, ct)
- ct = ct.to_i
- nr = nr.to_s
- size = nr.size
- if size < ct
- x = ct-size
- result = "0"*x+nr
- end
- if result != nil
- return result
- else
- return nr
- end
- end
- def self.check
- sel
- print "Started check mode\n"
- sel
- t = Time.now
- x = ARGV
- x[0] = nil
- result = []
- md5s = []
- duplicates = []
- log = ""
- count = 0
- duplicates_count = 0
- x.each{|y|
- next if y == nil || y == ""
- Find.find(y).each{|z|
- next if y == z
- lambda {
- count += 1
- cur_md5 = MD5Fixer.make_md5(z)
- if md5s.include?(cur_md5)
- log << "Duplicate: #{z} : #{cur_md5}\n"
- duplicates << [z,cur_md5]
- duplicates_count += 1
- else
- md5s << cur_md5
- result << [z,make_md5(z)]
- end
- print "#{duplicates_count}/#{count} #{z}\n"
- }.call rescue lambda {print "#{z} failed to check\n"; log << "#{z} failed to check\n"}.call
- }
- }
- result2 = ""
- duplicates.each{|x|
- result2 << "#{x[0]}\x00#{x[1]}\n"
- }
- tn = Time.now - t
- msg = "Check Complete!\nFiles checked: #{count}\nDuplicates: #{duplicates_count}\nTime needed: #{tn}\n"
- log << msg
- sel
- print msg
- sel
- File.open('md5fixer_duplicates.txt','wb'){|w|w.write(result2)}
- File.open('md5fixer_log.txt','wb'){|w|w.write(log)}
- end
- def self.fix
- sel
- print "Started fix mode\n"
- sel
- t = Time.now
- x = ARGV
- data = lambda{File.open(ARGV[1],'rb'){|f|return f.read}}.call
- data.each_line{|x|
- y = x.split("\x00")
- y[1] = y[1].gsub("\n","").gsub("\r","").gsub("\t","")
- if MD5Fixer.make_md5(y[0]) == y[1]
- File.delete(y[0])
- print "#{y[0]} deleted\n"
- else
- print "#{y[0]} MD5 not valid\n"
- end
- }
- sel
- print "Completed!\nTime needed: #{Time.now - t}\n"
- sel
- end
- def self.renumerate
- sel
- print "Started renumerate mode\n"
- sel
- t = Time.now
- x = ARGV
- nam = x[1]
- ct = x[2]
- x[0],x[1],x[2] = nil
- count = 0
- x.each{|y|
- next if y == nil || y == ""
- Find.find(y).each{|z|
- next if z == y
- lambda {
- ext = z.split(".")[-1]
- File.rename(z,"#{nam}#{MD5Fixer.make_number(count,ct)}.#{ext}")
- print "#{count} renamed\n"
- count += 1
- }.call rescue next
- }
- }
- sel
- print "Completed!\nTime needed: #{Time.now - t}\n"
- sel
- end
- end
- begin
- MD5Fixer.make_header
- if ARGV.size == 0
- print "Usage:\n\n"
- print sel
- print "MD5Fixer.rb c folder1 folder2 folder3\nTo check for duplicates\n"
- sel
- print "\n"
- sel
- print "MD5Fixer.rb f md5fixer_duplicates.txt\nTo remove duplicates\n"
- sel
- print "\n"
- sel
- print "MD5Fixer.rb r CAM 5\nTo renumerate files\nCAM is file pattern and 5 is number digit\n"
- sel
- print "\n"
- exit
- end
- mode = ARGV[0]
- case mode
- when "c"
- MD5Fixer.check
- when "f"
- MD5Fixer.fix
- when "r"
- MD5Fixer.renumerate
- end
- exit
- rescue => e
- print "Error : #{e}\n"
- exit
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement