> Could you provide an example please? The basenames are all unique!
Well, I guess you are doing a 'find' because the files are in various
directories, and you don't know which file is in which directory.
Otherwise, you'd be able to construct the path directly.
So at the simplest I'm thinking of something like this:
require 'find'
base2path = {}
Find.find("/etc") do |path|
base2path[File.basename(path)] = path
end
# have a look at what you've built
require 'pp'
pp base2path
You could add some logic to skip files you know are not of interest if
you like.
Then if the csv says it wants to rename foo.pdf, you look for
base2path["foo.pdf"] to find its full path instantly. If you get nil,
then it doesn't exist.
The disadvantage of what I've proposed above is that you have to scan
the whole filesystem once (very slow) before you start doing any work -
and that's probably the point where you find there are bugs in the rest
of your script, then you have to start again.
So it may be quicker to debug if you write the program to read the CSV
first, and then start doing the renames as you walk across the
filesystem:
idmap = {}
csvfile.each do |old_id, new_id|
idmap[old_id] = new_id
end
Find.find("/etc") do |path|
old_id = basename[path]
new_id = idmap[old_id]
next unless new_id
File.rename(path, File.join(File.dirname(path), new_id))
end
That's completely untested, but is just to give you an idea.
HTH,
Brian.
--
Posted via http://www.ruby-forum.com/.