# This  script  will copy the whole  source directory  tree
# to the  destination. Be carefull  that if the root target
# ( destination ) directory does not exists then it will be
# created. This script will work only on Windows platforms,
# but with some  small changes  will work on unix  as well.
# The destination files will be over  writen if they exist.
# Xxxxxx Xxxxxx
# xxxx@xxxxxxxx.xxxxx.xxx

my $source_directory = 'f:/temp' ;
my $target_directory = 'c:/temp' ;

&xcopy($source_directory,$target_directory);

############# xcopy subroutine #############

sub xcopy {
my ($pwd,$i)=($_[0],$i++);
die "You have not defined the \$target_directory variable, sorry...\n" if    $target_directory eq "";
die "You have not defined the \$source_directory variable, sorry...\n" if -d $source_directory !=1;
 if (( -d $target_directory !=1 ) && ( -e $target_directory ==1 ))
 {die "Can't continue because a file has target's dir name\n";}
 elsif ( -e $target_directory !=1 )
 { mkdir $target_directory || die "Couldn't create dir : $target_node\n"}
opendir ($i,$pwd) || die "Can't list $pwd\n";
while (my $source_node=readdir $i) {
 next if $source_node=~/^\.*$/;
$source_node       = $pwd.'/'.$source_node;
( my $relative_node ) = $source_node =~/$source_directory(.*)/;
$target_node       = $target_directory.$relative_node;
if (-d $source_node==1) {
 if (( -d $target_node !=1 ) && ( -e $target_node ==1 )) {
 die "Can't mkdir $target_node because a same name file exist\n" }
 elsif ( -e $target_node !=1 ) {
 print "mkdir  $target_node\n";
 mkdir $target_node || die "Couldn't create dir : $target_node\n" } }
else {
 if (( -d $target_node ==1 ) && ( -e $target_node ==1 )) {
 warn "Can't copy $target_node because a same name dir exist\n"; }
 else {
 print "coping $source_node to $target_node\n" if -e $target_node !=1 ;
 (my $copy_target_node) = $target_node; $copy_target_node=~s/\//\\/g  ;
 (my $copy_source_node) = $source_node; $copy_source_node=~s/\//\\/g  ;
 `$ENV{ComSpec} /c copy /b $copy_source_node $copy_target_node`;      }
}
&xcopy($source_node) if -d $source_node==1 }
closedir $i}


