递归删除目录

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

# encoding: utf-8

def recursive_delete(dir)
  files = []
  Dir.foreach(dir) do |fname|
    next if fname == '.' || fname == '..'
    path = dir + '/' + fname
    if File.directory?(path)
      puts "dir #{path}"
      recursive_delete(path)
    else
      puts "file #{path}"
      files << path
    end
  end
  files.each do |path|
    puts "delete file #{path}"
    #File.delete(path)
  end
  puts "delete dir #{dir}"
  Dir.rmdir(dir)
end

recursive_delete("D:\/sp")