Completed delete_dirs sub. Removed debugging and unneeded modules. Changed die to croak.

This commit is contained in:
Tracey Clark 2018-05-20 15:27:14 -05:00
commit 4b241d14f7

View file

@ -5,65 +5,39 @@ use warnings;
# Script to archive the financial data from the previous year # Script to archive the financial data from the previous year
use Time::Piece; use Time::Piece;
use File::Basename; use File::Basename;
use File::Find::Rule; # find all the subdirectories of a given directory use File::Find::Rule;
# http://search.cpan.org/~rclamp/File-Find-Rule-0.32/README
use File::Path qw(make_path remove_tree);
#use File::Copy::Recursive qw(fcopy rcopy dircopy fmove rmove dirmove);
use File::Copy::Recursive qw(dircopy); use File::Copy::Recursive qw(dircopy);
use List::MoreUtils qw(uniq);
use File::DirCompare; use File::DirCompare;
use Data::Dumper; use Carp qw( croak );
my $now = localtime; my $now = localtime;
my $year = $now->year; my $year = $now->year;
print "Year is $year\n"; print "Current year: $year\n";
# Get year to archive from current year # Get year to archive from current year
my $lastyear = $now->add_years(-1)->year; my $lastyear = $now->add_years(-1)->year;
print("Last year: $lastyear\n");
print("Last year was $lastyear\n");
# Define and create folders # Define and create folders
my (%files1, %files2);
my $financial_folder = '/home/tracey/Documents/financial/current_year/'; my $financial_folder = '/home/tracey/Documents/financial/current_year/';
my $financial_archive_folder = '/home/tracey/Documents/financial/1_financial_archives/'; my $financial_archive_folder = '/home/tracey/Documents/financial/1_financial_archives/';
my $lastyear_folder = $financial_archive_folder . $lastyear . 'FinancialArchives'; my $lastyear_folder = $financial_archive_folder . $lastyear . 'FinancialArchives';
my @folders = ( $financial_folder, $financial_archive_folder, $lastyear_folder ); my @folders = ( $financial_folder, $financial_archive_folder, $lastyear_folder );
=begin comment
# Will be using this kind of thing to get only files with current year for deletion
foreach my $folder ( @folders ) {
if ( -d $folder ) {
print("Folder exists: $folder\n");
}
unless( -d $folder ) {
print("Creating the folder \"$folder\"\n");
die "Could not create directory! $!" unless make_path($folder);
}
}
# Get current folder list for nonempty directories
my $find_rule = File::Find::Rule->new;
$find_rule->nonempty->maxdepth(1)->mindepth(1)->relative;
my @curr_subdirs = $find_rule->directory->in( $financial_folder );
=end comment
=cut
print("Copying current directories to archive folder\n"); print("Copying current directories to archive folder\n");
print("From: $financial_folder\n"); print("From: $financial_folder\n");
print("To: $lastyear_folder\n"); print("To: $lastyear_folder\n");
dircopy( $financial_folder, $lastyear_folder ) or die $!; dircopy( $financial_folder, $lastyear_folder ) or croak "Could not copy directories $!";
# Make sure contents match # Make sure contents match before we do any Deleting
my $same = compare_dirs( $financial_folder, $lastyear_folder ); # croak on errors to guard against data losss
print("The value of same is $same\n"); my $same_result = compare_dirs( $financial_folder, $lastyear_folder );
croak "Error: The folders are not the same!" unless ($same_result == 1) ;
die "Error: The folders are not the same!" unless ($same == 1) ;
# Delete all files from current year from archive folders # Delete all files from current year from archive folders
# Also delete files from last year in current year folder
delete_dirs( $financial_folder, $lastyear_folder, $year, $lastyear ); delete_dirs( $financial_folder, $lastyear_folder, $year, $lastyear );
# compare new directories to old directories to make sure they are the same
sub compare_dirs { sub compare_dirs {
my ( $dir1, $dir2 ) = @_; my ( $dir1, $dir2 ) = @_;
my $same = 1; my $same = 1;
@ -85,33 +59,22 @@ sub compare_dirs {
return $same; return $same;
} }
# Delete old files
sub delete_dirs { sub delete_dirs {
my ( $new_dir, $old_dir, $curr_year, $last_year ) = @_; my ( $new_dir, $old_dir, $curr_year, $last_year ) = @_;
print("Deleting files from current year in archives\n"); print("Deleting files from current year $curr_year in archives\n");
print("and files from last year from current folder\n"); print("and files from last year $last_year from current folder\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 @this_year_files = File::Find::Rule->file()
# my $find_rule = File::Find::Rule->new; ->name("*$curr_year*")
# # $find_rule->nonempty->maxdepth(5)->mindepth(1)->relative; ->in($old_dir);
# $find_rule->name('*.' . $lastyear . '*'); my @last_year_files = File::Find::Rule->file()
# my @curr_subdirs = $find_rule->directory->in( $new_dir ); ->name("*$last_year*")
# print("These are the current subdirs in delete_dirs\n\n"); ->in($new_dir);
# print Dumper(\@curr_subdirs);
# foreach my $subdir(@curr_subdirs) {
# print("Directory in current subdir is $subdir");
my @last_year_files = File::Find::Rule->file() unlink @this_year_files;
->name("*$curr_year*") print("Deleted all $curr_year files from the folder for $last_year\n");
->in($old_dir); unlink @last_year_files;
#print("These are the current files in last year folders in delete_dirs\n\n"); print("Deleted all $last_year files from the folder for $curr_year\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; return;
} }