require 'fileutils' class ReorgBot attr_accessor :path attr_accessor :dir def initialize(path) raise ArgumentError, "path '#{path}' does not exist!" unless File.exist?(path) raise ArgumentError, "path '#{path} is not a directory you dolt!" unless File.directory?(path) @path = path @dir = Dir.new(path) end def dirs @dir.select { |f| File.directory?(File.join(@path, f)) } end def empty_dirs dirs.select { |d| Dir[File.join(@path, d, '*')].empty? } end def non_empty_dirs dirs - empty_dirs end def remove_empty_dirs empty_dirs.each do |d| FileUtils.rm_rf(File.join(@path, d)) end end end bot = ReorgBot.new('/tmp/reorg_test') bot.remove_empty_dirs
You need to create an account or log in to post comments to this site.