HT copy a directory structure, and all files to a single director y

All-

Recently Matz and others advised the code below for recursively reading file names in a directory structure. (It works fine.)

Follow up questions:

– How can I expand this (or use related) code to copy the directory structure and files to a parallel directory where the target directories aren’t already there. (Eg, recreate the whole structure in “…/dupe_structure”.)

– A slightly different objective here: Assuming all files in the directories have unique names, how can I copy all files to a single target directory. (It seems to me this would involve parsing f somehow to extract just the file name. In the code below, f includes the path too.)

Thanks!

-Kurt

for f in Dir.glob("./**/*")
File.open(f) { |file|
puts f
do_more_stuff_here
} if File.file?(f)
end

directory…

Recently Matz and others advised the code below for recursively reading
file names in a directory structure. (It works fine.)

Follow up questions:

– How can I expand this (or use related) code to copy the directory
structure and files to a parallel directory where the target directories
aren’t already there. (Eg, recreate the whole structure in
"…/dupe_structure".)

– A slightly different objective here: Assuming all files in the
directories have unique names, how can I copy all files to a single target
directory. (It seems to me this would involve parsing f somehow to extract
just the file name. In the code below, f includes the path too.)

Some hints:

  1. File.basename will extract the filename
  2. File.dirname will extract the directory
  3. If you use the ftools library, there is
    a makepath (or something?) that will do the
    equivalent of mkdir -p on UNIX. Of course,
    if you make them only one level at a time,
    Dir.mkdir will probably be sufficient.

Hal

···

----- Original Message -----
From: “Kurt Euler” keuler@portal.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, August 30, 2002 11:42 PM
Subject: HT copy a directory structure, and all files to a single