By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

How to delete 1 week old files based on date created

P: 2
Can anyone help me to make a perl script that could delete 1 week old files based on the date created?

I made a folder: (August 16, 2008 to September 1, 2008)

08-16-08
08-17-08
08-18-08
08-19-08
08-20-08
08-21-08
08-22-08
08-23-08
08-24-08
08-25-08
08-26-08
08-27-08
08-28-08
08-29-08
08-30-08
08-31-08
09-01-08

I want to remain the folder from 09-01-08 to 08-26-08 and delete the old files from 08-25-08 to 08-16-08....

here's the scripts that i made..
Codes:

#!/usr/bin/perl

use Date::Pcalc qw(:all);

$cleanpath="/TestDelete";

($y, $m, $d)=Today();

$deduct=-12;

($y2, $m2, $d2) = Add_Delta_Days($y, $m, $d, $deduct);

$prevdate="$m2-$d2-$y2";

for ($i=0;$i<5;$i++)
{
$n=1;
($y2, $m2, $d2)= Add_Delta_Days($y2, $m2, $d2, $n);


if($d2==1 || $d2==2 || $d2==3 || $d2==4 || $d2==5 || $d2==6 || $d2==7 || $d2==8 || $d2==9)
{
$d2="0".$d2;
}

if ($m2<10)
{
$m2="0".$m2;
}

$shorty=substr($y2,2,3);

$ddate="$m2-$d2-$shorty";
`/usr/bin/find \"$cleanpath\" -name \"$ddate\" -exec rm -rf "{}" ';'`;
}

if i run my scripts it only deleted the folder :
08-21-08
08-22-08
08-23-08
08-24-08
08-25-08
but it should delete the 08-25-08 up to 08-16-08 :-(

I really need the scripts... thanks a lot...

perltest :-(
Sep 1 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Personally, I would have taken the dates and converted them to Unix time (which, if you don't know, is the number of seconds since January 1st, 1970.). If I remember right, its called epoch.

You get your reference which is todays date and convert it to epoch, then you use that result as a comparison to check the date of the other files.

Regards,

Jeff
Sep 1 '08 #2

P: 2
Personally, I would have taken the dates and converted them to Unix time (which, if you don't know, is the number of seconds since January 1st, 1970.). If I remember right, its called epoch.

You get your reference which is todays date and convert it to epoch, then you use that result as a comparison to check the date of the other files.

Regards,

Jeff
Hi Jeff,

Can u provide a perl script that can delete 1 week old files? or any for loop condition that can delete 1 week old files...?

Hope you can help me :-)
thanks

perltest :-)
Sep 1 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Hi Jeff,

Can u provide a perl script that can delete 1 week old files? or any for loop condition that can delete 1 week old files...?

Hope you can help me :-)
thanks

perltest :-)
I could, but you wouldn't learn anything from it and plus, this isn't a scripting service. Instead, we help people like yourself learn how to do this type of stuff by doing.

I won't write the script for you, but I can provide you an example of what I was referring to. Give me some time to put it together for you and I will post it here.

Regards,

Jeff
Sep 1 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.