Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleteing folders based on MMYY folder name,

Status
Not open for further replies.

wizzard99

MIS
Oct 14, 2003
4
GB
Hi all,

I need to create a script to delete some folders every month. I have a Folder that has sub folders named by company (These can change) and then under each of those are further sub folders called by MMYY values. Every month I want to run a script to delete folders older than 3 months, however I want to do this based on the folder name and not any created or modified time.

I've got this so far which should get me a list of the first level folders in the @Highfolders array and then the sub folders of each of this into the @Lowfolders_$hf array (I think).

my @HighFolders;
my $Highpath="D:/FSA/Recordings/";

use File::Util;
my($hf) = File::Util->new();
my(@HighFolders) = $hf->list_dir($Highpath,'--dirs-only');

foreach $hf(@Highfolders)
{
use File::Util;
my($lf) = File::Util->new();
my(@Lowfolders_$hf) = $lf->list_dir($lf,'--dirs-only');
}

I'm not sure how to do the date checking that I now need to do apart from just getting the current MM and YY figures then doing all the calculations necessary to work around year end. I'm sure there must be some simpler way of doing it so I am asking here to see if anyone can help.
Thanks.
 
I've been working on this and completely changed it so that now I have this,

use warnings;
use strict;
use File::Find::Rule;

my $path="D:/FSA/Recordings/";

my @folders = File::Find::Rule->directory->in( $path );

foreach my $i (@folders){
if ($i =~ /[0-9]{4}/)
{
print "$i\n";
my $folder = substr($i,-4,4);
print "$folder\n"};
}

This is successfully giving me a variable $folder which is the MMYY value that the folder is called. What I really need now is to work out how to compare that to current MMYY value and work out if it is over 3 months old.
 
Can you use the system date to get the current MMYY and then have a look at Date::Calc, which offers many methods. One of them might fit your needs.
 
Thanks,

I was able to get it working using Date::Calc once I worked out the format that the localtime gives you. This is what I ended up with.

Code:
use warnings;
use strict;
use File::Find::Rule;
use Date::Calc qw(Delta_Days);
use File::Path qw(remove_tree);
my $path="D:/FSA/Recordings/";

my @folders = File::Find::Rule->directory->in( $path );

foreach my $i (@folders){
    if ($i =~ /[0-9]{4}/)
        {
        my $folder = substr($i,-4,4);
        my $month = substr($folder,0,2);
        my $year = substr($folder,2,2);
        my $folderyear = "1" . $year;
        my @folderdate = ($folderyear, $month, 01);
        my($curday, $curmonth, $curyear) = (localtime)[3,4,5];
        my @curdate = (111, 2, 1);
        my $difference = Delta_Days(@folderdate, @curdate);
        if ($difference > 93) {remove_tree $i;}
        };
        }
 
Yuou could also do this, provided your years are all after 2000 (doesn't require the Calc module):
Code:
for(@folders){
  if(/^\d{4}$/){
    my$month=substr($_,0,2);
    my$year=substr($_,2);
    my($curmonth++,$curyear)=(localtime)[4,5];
    $curyear-=100;
    if(($curyear-$year)*12+$curmonth-$month>3){remove_tree $_}
  }
}
Note that I changed the regex to make it more robust: however mine assumes your directory names are strictly formed of 4 figures.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top