Modularize file processing, small improvements
This commit is contained in:
parent
c7b5feb111
commit
12dce591bb
2 changed files with 23 additions and 76 deletions
|
|
@ -1,87 +1,35 @@
|
|||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
use Modern::Perl;
|
||||
use ProcessDirectories;
|
||||
|
||||
# Script to archive the financial data from the previous year
|
||||
use Time::Piece;
|
||||
use File::Basename;
|
||||
use File::Find::Rule;
|
||||
use File::Copy::Recursive qw(dircopy);
|
||||
use File::DirCompare;
|
||||
use Carp qw( croak );
|
||||
# use File::Basename;
|
||||
# use File::Find::Rule;
|
||||
# use File::Copy::Recursive qw(dircopy);
|
||||
# use File::DirCompare;
|
||||
# use Getopt::Long qw(GetOptionsFromArray);
|
||||
use Time::Piece qw/localtime/;
|
||||
|
||||
my $now = localtime;
|
||||
my $year = $now->year;
|
||||
print "Current year: $year\n";
|
||||
|
||||
# Get year to archive from current year
|
||||
my $lastyear = $now->add_years(-1)->year;
|
||||
print("Last year: $lastyear\n");
|
||||
print("Current year: $year Last year: $lastyear\n");
|
||||
|
||||
# Define and create folders
|
||||
my $financial_folder = '/home/tracey/Documents/financial/current_year/';
|
||||
my $financial_archive_folder =
|
||||
'/home/tracey/Documents/financial/1_financial_archives/';
|
||||
my $lastyear_folder =
|
||||
$financial_archive_folder . $lastyear . 'FinancialArchives';
|
||||
my @folders =
|
||||
( $financial_folder, $financial_archive_folder, $lastyear_folder );
|
||||
my $financial_archive_folder = '/home/tracey/Documents/financial/1_financial_archives/';
|
||||
my $lastyear_folder = $financial_archive_folder . $lastyear . '_FinancialArchives';
|
||||
|
||||
print("Copying current directories to archive folder\n");
|
||||
print("From: $financial_folder\n");
|
||||
print("To: $lastyear_folder\n");
|
||||
dircopy( $financial_folder, $lastyear_folder )
|
||||
or croak "Could not copy directories $!";
|
||||
|
||||
# Make sure contents match before we do any Deleting
|
||||
# croak on errors to guard against data losss
|
||||
my $same_result = compare_dirs( $financial_folder, $lastyear_folder );
|
||||
croak "Error: The folders are not the same!" unless ( $same_result == 1 );
|
||||
create_archive_dirs ( $financial_folder, $lastyear_folder );
|
||||
|
||||
# Make sure contents match before we do any deleting
|
||||
# croak on errors to guard against data loss
|
||||
# my $same_result = compare_dirs( $financial_folder, $lastyear_folder );
|
||||
croak "Error: The source and destintion folder contents are not the same!" unless ( compare_dirs( $financial_folder, $lastyear_folder ) );
|
||||
|
||||
# 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 );
|
||||
clean_dirs( $financial_folder, $lastyear_folder, $year, $lastyear );
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
sub delete_dirs {
|
||||
my ( $new_dir, $old_dir, $curr_year, $last_year ) = @_;
|
||||
print("Deleting files from current year $curr_year in archives\n");
|
||||
print("and files from last year $last_year from current folder\n");
|
||||
|
||||
my @this_year_files =
|
||||
File::Find::Rule->file()->name("*$curr_year*")->in($old_dir);
|
||||
my @last_year_files =
|
||||
File::Find::Rule->file()->name("*$last_year*")->in($new_dir);
|
||||
|
||||
unlink @this_year_files;
|
||||
print("Deleted all $curr_year files from the folder for $last_year\n");
|
||||
unlink @last_year_files;
|
||||
print("Deleted all $last_year files from the folder for $curr_year\n");
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
#!/usr/bin/perl
|
||||
use strict;
|
||||
use warnings;
|
||||
use Modern::Perl;
|
||||
|
||||
use Time::Piece;
|
||||
use File::Find::Rule; # find all the subdirectories of a given directory
|
||||
|
|
@ -21,7 +20,7 @@ print("Last year was $lastyear and tax year was $lasttaxyear\n");
|
|||
# Define and create folders
|
||||
my ( $financial_folder, $financial_archive_folder, $lastyear_folder, $lasttaxyear_folder );
|
||||
$financial_folder = '/home/tracey/Documents/financial/current_year/';
|
||||
$financial_archive_folder = '~/Documents/financial/1_financial_archives/';
|
||||
$financial_archive_folder = '/home//Documents/financial/1_financial_archives/';
|
||||
$lastyear_folder = $financial_archive_folder . $lastyear . 'FinancialArchives';
|
||||
$lasttaxyear_folder = $financial_archive_folder . $lasttaxyear . 'TaxArchives';
|
||||
my @folders = ( $financial_folder, $financial_archive_folder, $lastyear_folder, $lasttaxyear_folder );
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue