     1     # This  script  will copy the whole  source directory  tree
     2     # to the  destination. Be carefull  that if the root target
     3     # ( destination ) directory does not exists then it will be
     4     # created. This script will work only on Windows platforms,
     5     # but with some  small changes  will work on unix  as well.
     6     # The destination files will be over  writen if they exist.
     7     # George Bouras
     8     # root@novastar.dtdns.net
     9     
    10     my $source_directory = 'f:/temp' ;
    11     my $target_directory = 'c:/temp' ;
    12     
    13     &xcopy($source_directory,$target_directory);
    14     
    15     ############# xcopy subroutine #############
    16     
    17     sub xcopy {
    18       my ($pwd,$i)=($_[0],$i++);
    19       die "You have not defined the \$target_directory variable, sorry...\n" 
    20         if $target_directory eq "";
    21       die "You have not defined the \$source_directory variable, sorry...\n" 
    22         unless -d $source_directory;
    23     
    24       if (! -d $target_directory &&  -e $target_directory) {
    25         die "Can't continue because a file has target's dir name\n";
    26       } elsif ( ! -e $target_directory ) {
    27         mkdir $target_directory || die "Couldn't create dir : $target_node\n";
    28       }
    29     
    30       opendir ($i,$pwd) || die "Can't list $pwd\n";
    31     
    32       while (my $source_node=readdir $i) {
    33         next if $source_node=~/^\.*$/;
    34         $source_node       = $pwd.'/'.$source_node;
    35         ( my $relative_node ) = $source_node =~/$source_directory(.*)/;
    36         $target_node       = $target_directory.$relative_node;
    37         if (-d $source_node) {
    38           if (! -d $target_node && -e $target_node) {
    39             die "Can't mkdir $target_node because a same name file exist\n";
    40           } elsif (! -e $target_node) {
    41             print "mkdir  $target_node\n";
    42             mkdir $target_node || die "Couldn't create dir : $target_node\n";
    43           }
    44         } else {
    45           if (-d $target_node && -e $target_node) {
    46             warn "Can't copy $target_node because a same name dir exist\n";
    47           } else {
    48             print "coping $source_node to $target_node\n" unless -e $target_node;
    49             (my $copy_target_node) = $target_node; $copy_target_node=~s/\//\\/g  ;
    50             (my $copy_source_node) = $source_node; $copy_source_node=~s/\//\\/g  ;
    51             `$ENV{ComSpec} /c copy /b $copy_source_node $copy_target_node`;
    52           }
    53         }
    54         &xcopy($source_node) if -d $source_node ;
    55       }
    56       closedir $i;
    57     }
    58     
    59     
