源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-09-29 22:28 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Ruby遍历文件夹同时计算文件的md5sum
#!/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)
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部