用户登录
用户注册

分享至

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

  • 作者: 左手面包右手段子
  • 来源: 51数据库
  • 2021-08-23
#!/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)

软件
前端设计
程序设计
Java相关