From: "David A. Black" Date: 2005-05-12T20:25:35+09:00 Subject: Re: Go through directories recursively HI -- On Thu, 12 May 2005, Jens Riedel wrote: > Hello, > > I'm quite new to Ruby. > I'd to walk through a directory tree and edit all jsp files in there. > > I do it this way: > > def editDirectory(path) > Dir.entries(path).each { |filename| > next if filename =~ /^\.+$/ > currFile = path + "/" + filename > if(File.stat(currFile).directory?) > editDirectory(currFile) > elsif(filename =~ /(.+)\.jsp$/) > # ... do something with the file > end > } > end > > I'd like to know if there is a more professional or more "ruby-like" way to > do this... You can use the 'find' module. Here's a parameterized version, which takes as its arguments a path and a file extension, and yields back the filenames it finds one by one: require 'find' def find_by_extension(path, ext) Find.find(path) do |f| next unless FileTest.file?(f) next unless /#{Regexp.escape(ext)}$/.match(f) yield f end end # Example of usage: find_by_extension("/home/dblack", ".rb") do |f| # do stuff with f end David -- David A. Black dblack@wobblini.net