Hi,
I have some directory trees like these
dir1 and
dir2 under directory
origin:
origin
dir1
subdir1
subdir2
subdir3
file1
file2
dir2
subdir1
subdir2
subdir3
file1
file2
that I want to copy under directory
destination. They contain both directories and files.
I came up with this where I can also set a parameter to specify the depth of replication, and it seems to work, but I wonder if there is a more elegant/shorter way to do this.
use strict;
use File::Copy;
use File::Basename;
use File::Path;
use File::Glob ':glob';
my $din = 'c:/temp/origin/';
my $dout = 'c:/temp/destination/';
docp($din,$dout,1);
sub docp {
my $din = shift(); # ORIGIN DIRECTORY
my $dout = shift(); # DESTINATION DIRECTORY
my $tree = shift(); # DEPTH OF DIRECTORY RECURSION
my ($f,$d,$b);
die if -e $dout;
mkpath($dout);
my @list = bsd_glob($din."*");
map {
($f,$d) = fileparse($_);
if (-d and $tree) {
$f = $f.'/';
$b = $_.'/';
docp($b,$dout.$f,$tree) if $tree ge 0;
} else {
copy ($_,$dout.$f);
}
} @list;
$tree = $tree - 1;
}
Thank you in advance.
roberto