# DIR_COPY and DIR_REMOVE http://groups-beta.google.com/group/comp.lang.perl.misc/browse_thread/thread/9b9368f0b9d9b65/6f20b3aedee645fe?q=recursive+move+perl+directory&rnum=5&hl=en#6f20b3aedee645fe use Cwd; use File::Path; use File::Basename; use File::Copy; &DIR_COPY ($source, $to); &DIR_REMOVE($source); # #------------------------------------------------#-------------------# sub DIR_COPY # D I R C O P Y # { #-------------------# # Recursively copy the indicated file or directory from one location # to another. my (@List, $rec); my ($file, $dest) = @_; my $name = basename($file); if ( -d $file) { mkdir ("$dest/$name", 0755); opendir (DIR, $file); while ( defined ( $rec= readdir (DIR ) ) ) { next if ( $rec =~ m/^\./ ); chomp; push (@List, "$file/$rec"); } close (DIR); foreach (@List) { &DIR_COPY($_, "$dest/$name"); } } else { copy ($file, "$dest/$name"); } } #end DIR_COPY #------------------------------------#---------------------------------# sub DIR_REMOVE # D I R R E M O V E # { #---------------------------------# # Recursively remove the package of directory, sub-directories & files. # Returns 1 if successful, 0 otherwise. my $file = shift; if( -d $file ) { # D I R E C T O R Y my @lower; opendir( DIR, $file ) || return( 0 ); # Failed to open while( defined ($_ = readdir( DIR )) ) { next if( m!^\.\.?$! ); # Skip '.' & '..' chomp; push( @lower, "$file/$_" ); # Collect lower files } #end while closedir( DIR ) || return( 0 ); # Failed to close foreach( @lower ) { return( 0 ) unless &DIR_REMOVE( $_ ); } return( rmdir( $file ) ); # Kill the directory } else # F I L E { return( unlink $file ); } # Kill the file } #end DIR_REMOVE