Recursive copy

G

gusmeister

Is there a Perl mod that has a recursive file copy function (similar to
`cp -r` in Unix or `xcopy` in Windows)? File::path does have a recursive
file deletion function (rmtree) but no recursive copy function (e.g.
cptree).
 
P

Phil Crow

gusmeister said:
Is there a Perl mod that has a recursive file copy function (similar to
`cp -r` in Unix or `xcopy` in Windows)? File::path does have a recursive
file deletion function (rmtree) but no recursive copy function (e.g.
cptree).

You could combine File::Find which recursively descends directories
and File::Copy which copies files. I can't be sure the following code
will work without knowing a bit more about how you want the
destination to look, but it might go something like this:

use File::Find;
use File::Copy;

sub visit {
my $file = $File::Find::name;
my $base = $file;
$base =~ s!/base/path/to/remove/!!;
copy($file, "/new/path/$base");
}

find({ wanted => \&visit, no_chdir => 1 }, '/base/path/to/remove');
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top