Path: monger.newsread.com!bad-news.newsread.com!netaxs.com!newsread.com!feeder.qis.net!dispose.news.demon.net!demon!newsfeed.icl.net!skynet.be!Amsterdam.Infonet!News.Amsterdam.UnisourceCS!news.otenet.gr!not-for-mail
From: "Otenet" <root@novastar.dtdns.net>
Newsgroups: comp.lang.perl.misc
Subject: Copy a directory structure
Date: Sat, 7 Apr 2001 15:54:41 +0200
Organization: An OTEnet S.A. customer
Lines: 50
Message-ID: <9an2mc$bd6$1@usenet.otenet.gr>
NNTP-Posting-Host: athe530-d120.otenet.gr
X-Trace: usenet.otenet.gr 986648077 11686 212.205.239.120 (7 Apr 2001 12:54:37 GMT)
X-Complaints-To: abuse@otenet.gr
NNTP-Posting-Date: Sat, 7 Apr 2001 12:54:37 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
Xref: bad-news.newsread.com comp.lang.perl.misc:387788


# 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.
# George Bouras
# root@novastar.dtdns.net

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}


