Forum: Ruby on Rails clue about deleting empty directories in a file structure ?

Posted by Kad Kerforn (kadoudal)
on 2012-11-21 18:37
(Received via mailing list)
I have been playing around w Dir and FileUtils, and I can delete the 
stuff
I want to get rid off , but then I get some empty folders
is there any way to remove ( recursively.. ) all empty folders ?

I found a piece of code dated 2007 ... is there anything new around ?

thanks for feedback
Posted by Kad Kerforn (kadoudal)
on 2012-11-21 18:50
(Received via mailing list)
[SOLVED]  found a better module extending FileUtils .. it works

module MyExtensions
  module FileUtils
    # Gracefully delete dirs that are empty (or contain empty children).
    def rmdir_empty(*dirs)
      dirs.each do |dir|
        begin
          ndel = Dir.glob("#{dir}/**/", File::FNM_DOTMATCH).count do |d|
            begin; Dir.rmdir d; rescue SystemCallError; end
          end
        end while ndel > 0
      end
    end
  end

  module ::FileUtils
    extend FileUtils
  end
end


Le mercredi 21 novembre 2012 18:35:57 UTC+1, Erwin a crit :
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.