On Thu, 12 May 2005, Robert Klemme wrote: > 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... >> >> Thanx for every hint, >> Jens > > require 'find' > > def edit_directory(dir) > Find::find(dir) do |f| > if /\.jsp$/i =~ f and File.file? f > # do something with f > end > end > end one thing to consider is that neither of these approaches follows links: harp:~/tmp > rm -rf * harp:~/tmp > mkdir a harp:~/tmp > touch a/b harp:~/tmp > ln -s /tmp/ c harp:~/tmp > ruby -r find -e 'Find::find("."){|f| p f}' "." "./c" "./a" "./a/b" using Dir#[] doesn't either: harp:~/tmp > ruby -r find -e 'p Dir["**/*"]' ["a", "a/b", "c"] use find2 from the RAA to follow links: harp:~/tmp > ruby -r find2 -e 'Find2::find(:follow=>true){|f| p f}' | head "." "./a" "./a/b" "./c" "./c/lost+found" "./c/.font-unix" "./c/.font-unix/fs7100" "./c/.306.80a5" "./c/.ICE-unix" "./c/.ICE-unix/dcop22125-1115045753" i've had both following and not-following links cause frustrating bugs - so it's good to be aware of which/when you need the behaviour. cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | renunciation is not getting rid of the things of this world, but accepting | that they pass away. --aitken roshi ===============================================================================