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

Count deleted files 1

Status
Not open for further replies.

sappleg

Programmer
Apr 15, 2008
31
CA
Hi Guys,

I am deleting 180 days old files from a directory. Now, I want the output that should count the number of files deleted. Here's my code:

if (chdir($storage_ABC_Dir))
{
foreach $FILES (<pabc*.*>)
{
if (-M "$storage_ABC_Dir/$FILES" > 180)
{
unlink("$storage_ABC_Dir/$FILES");
if ( $? != 0)
{
print "LOGIT_W. Can't cleanup files in $storage_ABC_Dir \n";
}
else
{
print "Total number of files deleted: \n";
}


How to achieve this please?
Any help is greatly appreciated.
Thanks in advance.
Sappleg
 
Untested:

Code:
my $cnt = 0;
if (chdir($storage_ABC_Dir)){
   foreach $FILES (<pabc*.*>){
      if (-M "$storage_ABC_Dir/$FILES" > 180){
          $cnt += unlink("$storage_ABC_Dir/$FILES") or do {
             print "LOGIT_W. Can't cleanup $FILES in $storage_ABC_Dir \n";
          }
      }
   }
}
print "Total deleted = $cnt\n";






------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
It worked. Thanks so much, Kevin.

Sappleg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top