shell-scripts/file_processing/archive_taxes.pl
2020-03-14 20:27:03 -05:00

65 lines
2.2 KiB
Perl
Executable file

#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;
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;
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;
my $lasttaxyear = $now->add_years(-2)->year;
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/';
$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 );
foreach my $folder ( @folders ) {
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 );
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: $!";
#}
}
# compare new directories to old directories to make sure they are the same
# Or DIE
# 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