Advertisement
Narzew

MD5 Fixer Source

Dec 21st, 2013
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.49 KB | None | 0 0
  1. require 'digest/md5'
  2. require 'find'
  3.  
  4. def sel
  5.     print "*************************************************************************\n"
  6. end
  7.  
  8. module MD5Fixer
  9.  
  10.     def self.make_header
  11.         print sel
  12.         print "**MD5Fixer v 1.00\n"
  13.         print "**Narzew\n"
  14.         print "**v 1.00\n"
  15.         print "**21.12.2013\n"
  16.         print "**All rights reserved.\n"
  17.         print sel
  18.         print "**Use at your own risk!\n"
  19.         print "**Make backup of data you use with this tool\n"
  20.         print sel
  21.     end
  22.  
  23.     def self.make_md5(x)
  24.         return lambda{File.open(x,'rb'){|f|return Digest::MD5.hexdigest(f.read)}}.call
  25.     rescue
  26.         print "#{x} MD5 checksum failed\n"
  27.     end
  28.    
  29.     def self.make_number(nr, ct)
  30.         ct = ct.to_i
  31.         nr = nr.to_s
  32.         size = nr.size
  33.         if size < ct
  34.             x = ct-size
  35.             result = "0"*x+nr
  36.         end
  37.         if result != nil
  38.             return result
  39.         else
  40.             return nr
  41.         end
  42.     end
  43.    
  44.     def self.check
  45.         sel
  46.         print "Started check mode\n"
  47.         sel
  48.         t = Time.now
  49.         x = ARGV
  50.         x[0] = nil
  51.         result = []
  52.         md5s = []
  53.         duplicates = []
  54.         log = ""
  55.         count = 0
  56.         duplicates_count = 0
  57.         x.each{|y|
  58.             next if y == nil || y == ""
  59.             Find.find(y).each{|z|
  60.                 next if y == z
  61.                 lambda {
  62.                 count += 1
  63.                 cur_md5 = MD5Fixer.make_md5(z)
  64.                 if md5s.include?(cur_md5)
  65.                     log << "Duplicate: #{z} : #{cur_md5}\n"
  66.                     duplicates << [z,cur_md5]
  67.                     duplicates_count += 1
  68.                 else
  69.                     md5s << cur_md5
  70.                     result << [z,make_md5(z)]
  71.                 end
  72.                 print "#{duplicates_count}/#{count} #{z}\n"
  73.                 }.call rescue lambda {print "#{z} failed to check\n"; log << "#{z} failed to check\n"}.call
  74.             }
  75.         }
  76.         result2 = ""
  77.         duplicates.each{|x|
  78.             result2 << "#{x[0]}\x00#{x[1]}\n"
  79.         }
  80.         tn = Time.now - t
  81.         msg = "Check Complete!\nFiles checked: #{count}\nDuplicates: #{duplicates_count}\nTime needed: #{tn}\n"
  82.         log << msg
  83.         sel
  84.         print msg
  85.         sel
  86.         File.open('md5fixer_duplicates.txt','wb'){|w|w.write(result2)}
  87.         File.open('md5fixer_log.txt','wb'){|w|w.write(log)}
  88.     end
  89.    
  90.     def self.fix
  91.         sel
  92.         print "Started fix mode\n"
  93.         sel
  94.         t = Time.now
  95.         x = ARGV
  96.         data = lambda{File.open(ARGV[1],'rb'){|f|return f.read}}.call
  97.         data.each_line{|x|
  98.             y = x.split("\x00")
  99.             y[1] = y[1].gsub("\n","").gsub("\r","").gsub("\t","")
  100.             if MD5Fixer.make_md5(y[0]) == y[1]
  101.                 File.delete(y[0])
  102.                 print "#{y[0]} deleted\n"
  103.             else
  104.                 print "#{y[0]} MD5 not valid\n"
  105.             end
  106.         }
  107.         sel
  108.         print "Completed!\nTime needed: #{Time.now - t}\n"
  109.         sel
  110.     end
  111.    
  112.     def self.renumerate
  113.         sel
  114.         print "Started renumerate mode\n"
  115.         sel
  116.         t = Time.now
  117.         x = ARGV
  118.         nam = x[1]
  119.         ct = x[2]
  120.         x[0],x[1],x[2] = nil
  121.         count = 0
  122.         x.each{|y|
  123.             next if y == nil || y == ""
  124.             Find.find(y).each{|z|
  125.                 next if z == y
  126.                 lambda {
  127.                 ext = z.split(".")[-1]
  128.                 File.rename(z,"#{nam}#{MD5Fixer.make_number(count,ct)}.#{ext}")
  129.                 print "#{count} renamed\n"
  130.                 count += 1
  131.                 }.call rescue next
  132.             }
  133.         }
  134.         sel
  135.         print "Completed!\nTime needed: #{Time.now - t}\n"
  136.         sel
  137.     end
  138.    
  139. end
  140.  
  141. begin
  142.     MD5Fixer.make_header
  143.     if ARGV.size == 0
  144.         print "Usage:\n\n"
  145.         print sel
  146.         print "MD5Fixer.rb c folder1 folder2 folder3\nTo check for duplicates\n"
  147.         sel
  148.         print "\n"
  149.         sel
  150.         print "MD5Fixer.rb f md5fixer_duplicates.txt\nTo remove duplicates\n"
  151.         sel
  152.         print "\n"
  153.         sel
  154.         print "MD5Fixer.rb r CAM 5\nTo renumerate files\nCAM is file pattern and 5 is number digit\n"
  155.         sel
  156.         print "\n"
  157.         exit
  158.     end
  159.     mode = ARGV[0]
  160.     case mode
  161.     when "c"
  162.         MD5Fixer.check
  163.     when "f"
  164.         MD5Fixer.fix
  165.     when "r"
  166.         MD5Fixer.renumerate
  167.     end
  168.     exit
  169. rescue => e
  170.     print "Error : #{e}\n"
  171.     exit
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement