dblack@wobblini.net wrote:
Hi --
[snip]
Anyway, the need has arisen to delete empty directory heirarchies (rsync will delete the files but not the directories because of the .svn subdirs). What I've done so far is this:
require 'fileutils'
def empty_dirs
@current.collect do |f|
f if (File.directory?(f) && Dir.glob("#{f}/*").empty?)
end.compact
enddef remove_empty_dirs
while not empty_dirs.empty?
empty_dirs.each {|d| FileUtils.rm_rf(d)}
end
end
[snip..asking for a better way]
You're currently using collect plus compact, but you can actually just
use select. (That's not a general equivalence, but in this case it
applies.)@current.select do |f|
File.directory?(f) && Dir["#{f}/*"].empty?
endI'd be inclined to break things out a little. (Untested code follows
-- please test on expendable datarequire 'fileutils'
def dirs
@current.select {|f| File.dir?(f) }
enddef empty_dirs
dirs.select {|d| Dir["#{d}/*"].empty? }
enddef non_empty_dirs
dirs - empty_dirs
enddef remove_empty_dirs
non_empty_dirs.each {|d| FileUtils.rm_rf(d) }
end
Hello,
Excellent! David, thanks very much. The extra reduction of tasks is exactly what I was looking for (it certainly is a skill that I am having difficulty mastering).
I changed the remove_empty_dirs to a) remove the empty, not non-empty, dirs (! :o), and b) modify the @current file list:
def remove_empty_dirs
until empty_dirs.empty?
empty_dirs.each do |d|
@current.delete(d)
FileUtils.rm_rf(d)
end
end
end
Again, thanks very much.
cheers,
paulv
···
On Wed, 29 Aug 2007, Paul van Delst wrote: