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)

Ruby常用文件操作代码实例
#建立一个222.rb文件并且输入字符file=File.open("222.rb","w+")file.puts"123nwadwan12124124ndwdw"file.close#输出222.rb的内容File.open("222.rb","r+")do|file|whileline=file.getsputsline

收集的多个ruby遍历文件夹代码实例
一、遍历文件夹下所有文件,输出文件名deftraverse_dir(file_path)ifFile.directoryfile_pathDir.foreach(file_path)do|file|iffile!="."andfile!=".."traverse_dir(file_path+"/"+file)endendels

Ruby常用文件操作方法
一、新建文件f=File.new(File.join("C:","Test.txt"),"w+")f.puts("IamJack")f.puts("HelloWorld")文件模式"r":Read-only.Startsatbeginningoffile(defaultmode)."r+":Read-write.Startsatbeginningof