首页 > 编程语言 > 详细

Ruby遍历文件夹同时计算文件的md5sum

时间:2016-01-30 15:15:31      阅读:309      评论:0      收藏:0      [点我收藏+]
#!/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)

Ruby遍历文件夹同时计算文件的md5sum

原文:http://www.jb51.net/article/66550.htm

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!