2018-01-20 22:05:05 -06:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
|
2018-03-19 21:20:20 -05:00
|
|
|
# Script to archive the financial data from the previous year
|
2018-01-20 22:05:05 -06:00
|
|
|
use Time::Piece;
|
2018-03-19 21:20:20 -05:00
|
|
|
use File::Basename;
|
2018-01-20 22:05:05 -06:00
|
|
|
use File::Find::Rule; # find all the subdirectories of a given directory
|
|
|
|
|
# http://search.cpan.org/~rclamp/File-Find-Rule-0.32/README
|
|
|
|
|
use File::Path qw(make_path remove_tree);
|
2018-03-19 21:20:20 -05:00
|
|
|
#use File::Copy;
|
2018-03-20 18:13:50 -05:00
|
|
|
#use File::Copy::Recursive qw(fcopy rcopy dircopy fmove rmove dirmove);
|
|
|
|
|
use File::Copy::Recursive qw(dircopy);
|
2018-03-19 21:20:20 -05:00
|
|
|
use List::MoreUtils qw(uniq);
|
2018-03-20 18:13:50 -05:00
|
|
|
use File::DirCompare;
|
2018-01-20 22:05:05 -06:00
|
|
|
|
|
|
|
|
my $now = localtime;
|
|
|
|
|
my $year = $now->year;
|
|
|
|
|
print "Year is $year\n";
|
|
|
|
|
|
|
|
|
|
# Get year to archive from current year
|
|
|
|
|
my $lastyear = $now->add_years(-1)->year;
|
|
|
|
|
|
2018-03-19 21:20:20 -05:00
|
|
|
print("Last year was $lastyear\n");
|
2018-01-20 22:05:05 -06:00
|
|
|
|
|
|
|
|
# Define and create folders
|
2018-03-19 21:20:20 -05:00
|
|
|
my (%files1, %files2);
|
2018-03-20 18:13:50 -05:00
|
|
|
my $financial_folder = '/home/tracey/Documents/financial/current_year/';
|
|
|
|
|
my $financial_archive_folder = '/home/tracey/Documents/financial/1_financial_archives/';
|
2018-03-19 21:20:20 -05:00
|
|
|
my $lastyear_folder = $financial_archive_folder . $lastyear . 'FinancialArchives';
|
|
|
|
|
my @folders = ( $financial_folder, $financial_archive_folder, $lastyear_folder );
|
2018-01-20 22:05:05 -06:00
|
|
|
|
2018-03-19 21:20:20 -05:00
|
|
|
=begin comment
|
|
|
|
|
# Will be using this kind of thing to get only files with current year for deletion
|
2018-01-20 22:05:05 -06:00
|
|
|
foreach my $folder ( @folders ) {
|
2018-03-19 21:20:20 -05:00
|
|
|
if ( -d $folder ) {
|
|
|
|
|
print("Folder exists: $folder\n");
|
|
|
|
|
}
|
2018-01-20 22:05:05 -06:00
|
|
|
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 );
|
2018-03-19 21:20:20 -05:00
|
|
|
=end comment
|
|
|
|
|
=cut
|
2018-01-20 22:05:05 -06:00
|
|
|
|
2018-03-19 21:20:20 -05:00
|
|
|
print("Copying current directories to archive folder\n");
|
|
|
|
|
print("From: $financial_folder\n");
|
|
|
|
|
print("To: $lastyear_folder\n");
|
2018-03-20 18:13:50 -05:00
|
|
|
dircopy($financial_folder, $lastyear_folder) or die $!;
|
2018-03-19 21:20:20 -05:00
|
|
|
|
|
|
|
|
# Make sure contents match
|
2018-03-20 18:13:50 -05:00
|
|
|
compare_dirs( $financial_folder, $lastyear_folder );
|
2018-01-20 22:05:05 -06:00
|
|
|
|
2018-03-19 21:20:20 -05:00
|
|
|
# Delete all files from current year from archive folders
|
|
|
|
|
|
|
|
|
|
# compare new directories to old directories to make sure they are the same
|
|
|
|
|
sub compare_dirs {
|
|
|
|
|
my ( $dir1, $dir2 ) = @_;
|
|
|
|
|
File::DirCompare->compare($dir1, $dir2, sub {
|
|
|
|
|
my ($a, $b) = @_;
|
|
|
|
|
if (! $b) {
|
|
|
|
|
printf "Only in %s: %s\n", dirname($a), basename($a);
|
|
|
|
|
} elsif (! $a) {
|
|
|
|
|
printf "Only in %s: %s\n", dirname($b), basename($b);
|
|
|
|
|
} else {
|
|
|
|
|
print "Files $a and $b differ\n";
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-01-20 22:05:05 -06:00
|
|
|
|
2018-03-19 21:20:20 -05:00
|
|
|
# Delete old directory
|
2018-03-20 18:13:50 -05:00
|
|
|
sub delete_dirs {
|
|
|
|
|
# For all files in archive dir that are newer than last year delete them
|
|
|
|
|
|
|
|
|
|
# For all file in current folder that are older than this year delete them
|
|
|
|
|
}
|