From: Robert Klemme Date: 2004-04-10T06:14:16+09:00 Subject: Re: File.rename weirdness (was Re: need help with recursion) "David A. Black" schrieb im Newsbeitrag news:Pine.LNX.4.44.0404090846010.18453-100000@wobblini... > Hi -- > > On Sat, 10 Apr 2004, Robert Klemme wrote: > > > Dir[ File.join( dir, "**", "*" ) ].each do |name| > > d, base = File.split name > > changed = base.dup.gsub!("_", " ") and > > File.rename( name, File.join( d, changed ) ) > > end > > > > The same is possible with Find.find of course. In fact, File.find might be > > more efficient if the hieararchy contains many files. > > Yes, actually I used Find.find in my example :-) I know. I included the other variant for the purpose of showing a different approach. :-) > You're right about > the overzealous gsub'ing. I'm not sure how directories were supposed > to be handled, but I thought I was ignoring them, which I accidentally > wasn't. Me, too. But that's an easy change: Dir[ File.join( dir, "**", "*" ) ].each do |name| if File.file? name d, base = File.split name changed = base.dup.gsub!("_", " ") and File.rename( name, File.join( d, changed ) ) end end or Dir[ File.join( dir, "**", "*" ) ].each do |name| next unless File.file? name d, base = File.split name changed = base.dup.gsub!("_", " ") and File.rename( name, File.join( d, changed ) ) end Note: even when directories are ignored for the rename, the gsub on the complete name still can do harm and should by replaced by something along the lines of my suggestion (File.split and File.join). Regards robert