Added working copy routine
This commit is contained in:
parent
0367c0f223
commit
1547fc9797
1 changed files with 45 additions and 30 deletions
|
|
@ -2,11 +2,15 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
# Script to archive the financial data from the previous year
|
||||
use Time::Piece;
|
||||
use File::Basename;
|
||||
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);
|
||||
use File::Copy;
|
||||
#use File::Copy;
|
||||
use File::Copy::Recursive qw(fcopy rcopy dircopy fmove rmove dirmove);
|
||||
use List::MoreUtils qw(uniq);
|
||||
|
||||
my $now = localtime;
|
||||
my $year = $now->year;
|
||||
|
|
@ -14,19 +18,22 @@ print "Year is $year\n";
|
|||
|
||||
# Get year to archive from current year
|
||||
my $lastyear = $now->add_years(-1)->year;
|
||||
my $lasttaxyear = $now->add_years(-2)->year;
|
||||
|
||||
print("Last year was $lastyear and tax year was $lasttaxyear\n");
|
||||
print("Last year was $lastyear\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/';
|
||||
$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 );
|
||||
my (%files1, %files2);
|
||||
my $financial_folder = '~/Documents/financial/current_year';
|
||||
my $financial_archive_folder = '~/Documents/financial/1_financial_archives/';
|
||||
my $lastyear_folder = $financial_archive_folder . $lastyear . 'FinancialArchives';
|
||||
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);
|
||||
|
|
@ -37,29 +44,37 @@ foreach my $folder ( @folders ) {
|
|||
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 archives folder\n");
|
||||
foreach my $curr_subdir (@curr_subdirs) {
|
||||
my $targetdir = $lastyear_folder . "/" . $curr_subdir;
|
||||
print ("Target dir is $targetdir\n");
|
||||
#unless( -d $targetdir ){
|
||||
print("Copying directory \"$curr_subdir\"\n");
|
||||
print("Will copy to " . $targetdir . "/\n");
|
||||
# copy directories to archive directories the perl way
|
||||
#copy( $curr_subdir, $lastyear_folder ) or die "Copy failed: $!";
|
||||
#}
|
||||
}
|
||||
|
||||
print("Copying current directories to archive folder\n");
|
||||
print("From: $financial_folder\n");
|
||||
print("To: $lastyear_folder\n");
|
||||
|
||||
system('cp -rp ' . $financial_folder . '/* ' . $lastyear_folder);
|
||||
die("Can't launch cp: $!\n") if $? == -1;
|
||||
die("cp killed by signal ".($? & 0x7F)."\n") if $? & 0x7F;
|
||||
die("cp exited with error ".($? >> 8)."\n") if $? >> 8;
|
||||
|
||||
# Make sure contents match
|
||||
#compare_dirs( $curr_subdir, $lastyear_folder );
|
||||
|
||||
# Delete all files from current year from archive folders
|
||||
|
||||
# compare new directories to old directories to make sure they are the same
|
||||
# Or DIE
|
||||
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";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
# For each CURR folder
|
||||
# For each file
|
||||
# If mtime year < this year
|
||||
# Delete file
|
||||
|
||||
# For each ARCH folder
|
||||
# For each file
|
||||
# If mtime year > last year
|
||||
# Delete file
|
||||
# Delete old directory
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue