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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete old files using dos commands 1

Status
Not open for further replies.

rozzay

Programmer
Jan 3, 2002
142
US
Morning,

I am trying to use perl to delete old files in different folder files. I was able to do this but only for one folder. I am trying to have the perl to go and find the files in 1 folder delete and then go to another folder and then delete the files. When running the perl script it appears to find the fils okay but deletes only in the 2nd folder. Did I end something wrong?
Thanks in advance.

#!/usr/bin/perl
use File::Copy;
## CLEAN-UP FOR TS214IB
$dir_TS214IB = "/ISD-EDI/TS214IB";
$retention_period_TS214IB = "15";
## OPEN AND READ THE DIRECTORY
opendir (DIR, "$dir_TS214IB/");
@FILES = grep(/a*.*/,readdir(DIR));
closedir (DIR);
## DELETE THE .EDI FILES THAT ARE OLDER THAN X NBR OF DAYS
foreach $FILES (@FILES)
{
if (-M "$dir_TS214IB/$FILES" > $retention_period_TS214IB)
{
print "$FILES\n";
unlink("$dir_TS214IB_BK/$FILES");
}
}
## CLEAN-UP FOR TS214IB-BK
$dir_TS214IB_BK = "/ISD-EDI/TS214IB-BK";
$retention_period_TS214IB_BK = "15";
## OPEN AND READ THE DIRECTORY
opendir (DIR, "$dir_TS214IB_BK/");
@FILES = grep(/bk*.*/,readdir(DIR));
closedir (DIR);
## DELETE THE .EDI FILES THAT ARE OLDER THAN X NBR OF DAYS
foreach $FILES (@FILES)
{
if (-M "$dir_TS214IB_BK/$FILES" > $retention_period_TS214IB_BK)
{
print "$FILES\n";
unlink("$dir_TS214IB_BK/$FILES");
}
}
 
Your not putting traces on your actions to a trace file.

unlink("$dir_TS214IB_BK/$FILES") || print STDERR "\nCouldn't unlink $dir_TS214IB_BK/$FILES, $!";

You could open STDERR at the beginning of your file to write the traces.
BEGIN{open(STDERR, ">./err.txt");}

Thanks
Pat
ideas@microwebber.com
 
Hi Pat,

I am facing the similar situation as rozzay. I am able to delete *.txt files older then 2 days in a folder but unable delete the *.txt in the subfolders.

Can you please help, where exactly should we insert the trace file in the script.

Thanks so much,

 
File::Find is a good way to get access to sub-directories

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top