If you are simply copying and not doing any processing on the files, there is no reason not to use xcopy
.
Now, I wrote the script below in light of Telemachus's comments to provide you with a starting point. I personally would stick with xcopy
for copying and File::Find
if file contents need to be processed. Besides, I am sure there are about 37 bugs in the code below. But, if you want to play around, it might be instructive:
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec::Functions qw( catfile );
die "mydeepcp src-dir target-dir\n" unless @ARGV == 2;
my ($src, $target) = @ARGV;
mydeepcp( $src => $target );
sub mydeepcp {
my ($src, $target) = @_;
opendir my $dir_h, $src
or die "Cannot open directory: '$src': $!";
while ( my $file = readdir $dir_h ) {
next if $file =~ m{^\.\.?$};
my $src_path = catfile $src => $file;
my $target_path = catfile $target => $file;
if ( -d $src_path ) {
# FIXME: insert code somewhere to create destination dir
mydeepcp($src_path => $target_path);
}
elsif ( -f _ ) {
if ( my $err = docp($src_path => $target_path) ) {
warn sprintf(
"Error copying '%s' from '%s' to '%s': %s\n",
$file, $src, $target, $err
);
}
}
else {
warn "Skipping '$src_path'\n";
}
}
closedir $dir_h;
return;
}
sub docp {
my ($src, $target) = @_;
warn "'$src' => '$target'\n";
return;
}
__END__
Output:
C:\Temp> ghj c:\windows f:\qwert
...
'C:\windows\$hf_mig$\KB899591\update\spcustom.dll' => 'F:\qwert\$hf_mig$\KB899591\update\spcustom.dll'
'C:\windows\$hf_mig$\KB899591\update\update.exe' => 'F:\qwert\$hf_mig$\KB899591\update\update.exe'
'C:\windows\$hf_mig$\KB899591\update\update.ver' => 'F:\qwert\$hf_mig$\KB899591\update\update.ver'