From: Massimiliano Mirra Date: 2002-02-28T19:32:50+09:00 Subject: Re: [Q] Directory copy recursively On Thu, Feb 28, 2002 at 09:10:05AM +0900, roktas wrote: > Unfortunately, I am new to Ruby :( (like you?) Of course I have enough > time, but seeing and respecting all those gurus walking around here, I > am not the right person to advise (at least for the moment). Hope, > someone will follow the thread and feed the required help :) BTW, have I'm not a guru, but I hope the following will help. I whipped it together just a couple of days ago to provide recursive copy for rpkg, without platform dependence and without it complaining if part of the target tree exists. It addresses the problem of relative instead of absolute paths raised by kwatch, too; it works transparently if source is a file, and does its best to make you end up in the same directory where you started from, which is important if you are doing a lot of file handling actions in sequence. It doesn't handle permissions and patterns (I didn't need either). def File.cp_r(from, to) if FileTest.file?(from) File.cp(from, to) return end start_dir = Dir.pwd from, to = File.expand_path(from), File.expand_path(to) from_contents = Dir[File.join(from, '*')] puts "mkpath #{to}" if $DEBUG File.mkpath(to) puts "cd #{to}" if $DEBUG Dir.chdir(to) from_contents.each do |entry| name = File.basename(entry) if FileTest.directory?(entry) puts "mkpath #{name}" if $DEBUG File.mkpath(name) puts "cp_r #{entry} #{name}" if $DEBUG cp_r(entry, name) else puts "cp #{entry} #{name}" if $DEBUG File.cp(entry, name) end end Dir.chdir(start_dir) end