Clarified output for dir comparison. Finished sub to delete redundant files after copy.

Added die if folders are not the same after copy.
This commit is contained in:
Tracey Clark 2018-05-20 14:56:42 -05:00
commit a30dd12ce6

View file

@ -55,7 +55,10 @@ print("To: $lastyear_folder\n");
dircopy( $financial_folder, $lastyear_folder ) or die $!;
# Make sure contents match
compare_dirs( $financial_folder, $lastyear_folder );
my $same = compare_dirs( $financial_folder, $lastyear_folder );
print("The value of same is $same\n");
die "Error: The folders are not the same!" unless ($same == 1) ;
# Delete all files from current year from archive folders
delete_dirs( $financial_folder, $lastyear_folder, $year, $lastyear );
@ -63,17 +66,23 @@ delete_dirs( $financial_folder, $lastyear_folder, $year, $lastyear );
# compare new directories to old directories to make sure they are the same
sub compare_dirs {
my ( $dir1, $dir2 ) = @_;
my $same = 1;
print("Making sure the copy was complete and all files match\n");
File::DirCompare->compare($dir1, $dir2, sub {
my ($a, $b) = @_;
if (! $b) {
printf "Only in %s: %s\n", dirname($a), basename($a);
$same = 0;
} elsif (! $a) {
printf "Only in %s: %s\n", dirname($b), basename($b);
$same = 0;
} else {
print "Files $a and $b differ\n";
$same = 0;
}
});
print("Diff of directories done\n");
return $same;
}
# Delete old files
@ -81,17 +90,28 @@ sub delete_dirs {
my ( $new_dir, $old_dir, $curr_year, $last_year ) = @_;
print("Deleting files from current year in archives\n");
print("and files from last year from current folder\n");
print("New dir in the delete_dirs is $new_dir\n");
# print("New dir in the delete_dirs is $new_dir\n");
# print("Old dir in the delete_dirs is $old_dir\n");
# For all files in archive dir that are newer than last year delete them
my $find_rule = File::Find::Rule->new;
# $find_rule->nonempty->maxdepth(1)->mindepth(1)->relative;
$find_rule->name('*.' . $lastyear . '*');
my @curr_subdirs = $find_rule->directory->in( $new_dir );
print("This is the current subdirs in delete_dirs\n\n");
print Dumper(\@curr_subdirs);
foreach my $subdir(@curr_subdirs) {
print("Directory in current subdir is $subdir");
}
# my $find_rule = File::Find::Rule->new;
# # $find_rule->nonempty->maxdepth(5)->mindepth(1)->relative;
# $find_rule->name('*.' . $lastyear . '*');
# my @curr_subdirs = $find_rule->directory->in( $new_dir );
# print("These are the current subdirs in delete_dirs\n\n");
# print Dumper(\@curr_subdirs);
# foreach my $subdir(@curr_subdirs) {
# print("Directory in current subdir is $subdir");
my @last_year_files = File::Find::Rule->file()
->name("*$curr_year*")
->in($old_dir);
#print("These are the current files in last year folders in delete_dirs\n\n");
#print Dumper(\@last_year_files);
my $num_files = scalar(@last_year_files);
print("Number of current files in last year folders in delete_dirs: $num_files\n\n");
# For all file in current folder that are older than this year delete them
return;
}