Directory tree traversal fun

J

Jerry Blanco

Hi,
I want to write a recursive file renamer. It will work such that given
the following:

c:\prov\foo1.txt
c:\prov\fooDir
c:\prov\fooDir\foo2.txt

you call rename("c:\\prov", "foo", "bar") and get

c:\prov\bar1.txt
c:\prov\barDir
c:\prov\barDir\bar2.txt

I wrote the following:

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


The problem is that p.find() is top-to-bottom, so it renames in the
following order:
1. c:\prov\bar1.txt
2. c:\prov\barDir

Then, it can't get to c:\prov\fooDir anymore, so it doesn't rename
c:\prov\fooDir\foo2.txt

Is there a more elegant solution than creating an array with the
names, sort the array length descending and rename? I don't trust this
approach because it could happen that it reverts the order of some
files.

Is there a way to traverse a directory leaves-first? (bottom-to-top)
 
Z

Zachary Holt

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top