use strict; use warnings; use File::Glob ':bsd_glob'; ## This module is not included in ActiveState Perl so run this script with d:\portableperl\portableshell.bat. use File::Copy::Recursive qw(rcopy); #local $File::Copy::Recursive::SkipFlop = 1; my $logfile = 'sync_study_to_prod_test.log'; open my $fh_log, ">", $logfile or die "Couldn't open $logfile for output: $!\n"; print $fh_log "Ran at: ", scalar localtime, "\n"; print "Ran at: ", scalar localtime, "\n"; if( not defined $ENV{SERVER_ENVIRONMENT} or not $ENV{SERVER_ENVIRONMENT} ) { print $fh_log "Environment variable \"SERVER_ENVIRONMENT\" is required to be set up. \n"; die "Environment variable \"SERVER_ENVIRONMENT\" is required to be set up. \n"; } while ( ) { chomp; #skip blank lines next if /^\s*$/; my ($source,$destination) = split /\|/; ## trim whitespace at start or end of paths: $source =~ s/^\s+//; $destination =~ s/^\s+//; $source =~ s/\s+$//; $destination =~ s/\s+$//; ## Resolve environment variables $source =~ s/\%(.+?)\%/$ENV{$1}/g; $destination =~ s/\%(.+?)\%/$ENV{$1}/g; ## verify paths if ( not -e $source ) { print $fh_log "s-Warning: $source not found - skipping.\n"; next; } if ( not -e $destination ) { print $fh_log "d-Warning: $destination not found - skipping.\n"; next; } clear_readonly_bit($source); clear_readonly_bit($destination); my ($num_of_files_and_dirs,$num_of_dirs,$depth_traversed) = rcopy ( $source, $destination); if($num_of_files_and_dirs == 0) { print $fh_log "Error: Couldn't copy $source to $destination : $! -\n"; print $fh_log "--$num_of_files_and_dirs,$num_of_dirs,$depth_traversed\n"; next; } print $fh_log "****Copied $source to $destination\n ($num_of_files_and_dirs files and directories, $num_of_dirs directories, depth = $depth_traversed)\n"; } print $fh_log "Finished at: ", scalar localtime, "\n"; sub clear_readonly_bit { my $folder = shift; foreach my $file ( glob( "$folder/*" )) { #print $fh_log "file: $file\n"; ## test if not writable if( not -w $file ){ print $fh_log "$file is read only.\n--Clearing read-only bit...\n"; ## clear read only bit my $cnt = chmod 0777, $file; if( $cnt == 0){ print $fh_log "--Error-Couldn't clear read-only bit.\n"; } } if ( -d $file ) { ## clear ro bits in files in sub folders. clear_readonly_bit($file); } } } __DATA__ D:\Eterra\Distribution\FileUpdaterServer\%SERVER_ENVIRONMENT%\Bitmaps | %IDMS_ROOT%\bitmaps