Tidied
This commit is contained in:
parent
4b241d14f7
commit
c14c180bfe
1 changed files with 47 additions and 40 deletions
|
|
@ -10,71 +10,78 @@ use File::Copy::Recursive qw(dircopy);
|
||||||
use File::DirCompare;
|
use File::DirCompare;
|
||||||
use Carp qw( croak );
|
use Carp qw( croak );
|
||||||
|
|
||||||
my $now = localtime;
|
my $now = localtime;
|
||||||
my $year = $now->year;
|
my $year = $now->year;
|
||||||
print "Current year: $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: $lastyear\n");
|
||||||
|
|
||||||
# Define and create folders
|
# Define and create folders
|
||||||
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 =
|
||||||
my $lastyear_folder = $financial_archive_folder . $lastyear . 'FinancialArchives';
|
'/home/tracey/Documents/financial/1_financial_archives/';
|
||||||
my @folders = ( $financial_folder, $financial_archive_folder, $lastyear_folder );
|
my $lastyear_folder =
|
||||||
|
$financial_archive_folder . $lastyear . 'FinancialArchives';
|
||||||
|
my @folders =
|
||||||
|
( $financial_folder, $financial_archive_folder, $lastyear_folder );
|
||||||
|
|
||||||
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 croak "Could not copy directories $!";
|
dircopy( $financial_folder, $lastyear_folder )
|
||||||
|
or croak "Could not copy directories $!";
|
||||||
|
|
||||||
# Make sure contents match before we do any Deleting
|
# Make sure contents match before we do any Deleting
|
||||||
# croak on errors to guard against data losss
|
# croak on errors to guard against data losss
|
||||||
my $same_result = compare_dirs( $financial_folder, $lastyear_folder );
|
my $same_result = compare_dirs( $financial_folder, $lastyear_folder );
|
||||||
croak "Error: The folders are not the same!" unless ($same_result == 1) ;
|
croak "Error: The folders are not the same!" unless ( $same_result == 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
|
# 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 );
|
||||||
|
|
||||||
sub compare_dirs {
|
sub compare_dirs {
|
||||||
my ( $dir1, $dir2 ) = @_;
|
my ( $dir1, $dir2 ) = @_;
|
||||||
my $same = 1;
|
my $same = 1;
|
||||||
print("Making sure the copy was complete and all files match\n");
|
print("Making sure the copy was complete and all files match\n");
|
||||||
File::DirCompare->compare($dir1, $dir2, sub {
|
File::DirCompare->compare(
|
||||||
my ($a, $b) = @_;
|
$dir1, $dir2,
|
||||||
if (! $b) {
|
sub {
|
||||||
printf "Only in %s: %s\n", dirname($a), basename($a);
|
my ( $a, $b ) = @_;
|
||||||
$same = 0;
|
if ( !$b ) {
|
||||||
} elsif (! $a) {
|
printf "Only in %s: %s\n", dirname($a), basename($a);
|
||||||
printf "Only in %s: %s\n", dirname($b), basename($b);
|
$same = 0;
|
||||||
$same = 0;
|
}
|
||||||
} else {
|
elsif ( !$a ) {
|
||||||
print "Files $a and $b differ\n";
|
printf "Only in %s: %s\n", dirname($b), basename($b);
|
||||||
$same = 0;
|
$same = 0;
|
||||||
}
|
}
|
||||||
});
|
else {
|
||||||
print("Diff of directories done\n");
|
print "Files $a and $b differ\n";
|
||||||
return $same;
|
$same = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
print("Diff of directories done\n");
|
||||||
|
return $same;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 $curr_year in archives\n");
|
print("Deleting files from current year $curr_year in archives\n");
|
||||||
print("and files from last year $last_year from current folder\n");
|
print("and files from last year $last_year from current folder\n");
|
||||||
|
|
||||||
my @this_year_files = File::Find::Rule->file()
|
my @this_year_files =
|
||||||
->name("*$curr_year*")
|
File::Find::Rule->file()->name("*$curr_year*")->in($old_dir);
|
||||||
->in($old_dir);
|
my @last_year_files =
|
||||||
my @last_year_files = File::Find::Rule->file()
|
File::Find::Rule->file()->name("*$last_year*")->in($new_dir);
|
||||||
->name("*$last_year*")
|
|
||||||
->in($new_dir);
|
|
||||||
|
|
||||||
unlink @this_year_files;
|
unlink @this_year_files;
|
||||||
print("Deleted all $curr_year files from the folder for $last_year\n");
|
print("Deleted all $curr_year files from the folder for $last_year\n");
|
||||||
unlink @last_year_files;
|
unlink @last_year_files;
|
||||||
print("Deleted all $last_year files from the folder for $curr_year\n");
|
print("Deleted all $last_year files from the folder for $curr_year\n");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue