Ruby遍历文件夹同时计算文件的md5sum
程序员文章站
2022-04-28 22:23:27
#!/usr/bin/ruby -w
#
require 'digest/md5'
if argv.empty?
puts "usgae...
#!/usr/bin/ruby -w # require 'digest/md5' if argv.empty? puts "usgae: #$0 path" exit 0 end dir_name=argv.shift def dir_md5sum(path) md5s=array.new if file.directory?(path) dir.new(path).each do |file| next if file =~ /^\.+$/ file="#{path}/#{file}" if file.directory?(file) dir_md5sum(file) elsif file.file?(file) md5="#{digest::md5.hexdigest(file.read(file))} #{file}" md5s.push(md5) end end elsif file.file?(path) md5="#{digest::md5.hexdigest(file.read(path))} #{path}" md5s.push(md5) else puts "ivalid file type" exit 2 end md5s.each do |item| puts item end end dir_md5sum(dir_name)