From: Zachary Holt Date: 2007-06-15T06:57:52+09:00 Subject: Re: Directory tree traversal fun On Jun 14, 2007, at 1:47 PM, Jerry Blanco wrote: > Hi, Hi. > def rename(headOfTree, what, withWhat) > p = Pathname.new(headOfTree) > p.find() do |file| > path = file.to_s #just to make it prettier > File.rename(path, path.gsub("#{what}", withWhat)) if path.include? > (what) > end > end > Is there a way to traverse a directory leaves-first? (bottom-to-top) def rename( headOfTree, what, withWhat ) p = Pathname.new( headOfTree ) p.each_entry do |file| path = file.to_s #just to make it prettier next if path =~ /^\.\.?$/ rename( path, what, withWhat ) if path.directory? File.rename( path, path.gsub( "#{what}", withWhat ) ) if path.include?( what ) end end This code is untested, but if you recurse before you rename, you should get what you want.