From: Jim Bob Date: 2003-08-17T04:16:36+09:00 Subject: [RCR] Dir.recurse I think it'd be useful (and worthwhile) to have a Dir.recurse method. This method would take two optional arguments and a mandatory block. The first argument, if given, is the directory to start recursing from. If it is omitted, then recursion starts from the current directory. The second argument, if given, specifies a maximum depth to recurse to. If omitted, recursion would go as deep as possible. The method would change to each directory under the current working directory in turn, and also pass each directory's name to the block. The block would be executed once for each directory. So, given a directory tree like somedir / \ sub sub2 / \ ssub ssub2 'Dir.recurse("/somedir") { |x| puts x }' would print: /somedir /somedir/sub /somedir/sub/ssub /somedir/sub/ssub2 /somedir/sub2 (though the order could differ, of course.) Likewise, 'Dir.recurse("/somedir", 1) { puts Dir.pwd }' would print: /somedir /somedir/sub /somedir/sub2 i.e., only going one directory below the start directory. Of course, in actual usage, you'd probably be more often interested in the files in the directories than in the directories themselves. Below is an example of how this could be implemented. I'm sure it needs work, but I provide it just to give the general idea of what I am talking about. -------------------------------------------------- class Dir def Dir.recurse(d=".", maxdepth=nil, &block) raise ArgumentError unless block_given? # If "maxdepth" is not specified, recurse as deeply as possible if maxdepth return 0 if maxdepth < 0 maxdepth -= 1 end # Save working directory so it can be restored. start = Dir.pwd d = File.expand_path(d) begin Dir.chdir(d) block.call(d) Dir.entries(d).each do |f| next unless (FileTest.directory? f) && (f !~ /^\.{1,2}$/) Dir.recurse(f, maxdepth, &block) end # Rescue "permission denied" errors, so that recurse continues and # working directory is correctly restored. rescue Errno::EACCES $stderr.puts $! end Dir.chdir(start) end end --------------------------------------------------