当前位置: 移动技术网 > IT编程>脚本编程>Ruby > Ruby遍历文件夹同时计算文件的md5sum

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

2017年12月08日  | 移动技术网IT编程  | 我要评论
#!/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)

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网