Below is a script used for checking a directory and all sub directories for anything that is over 30 days old. If the item is found to be older than 30 days then it gets deleted (this is on a win32 system *choke* using Active Perl).

This is used on a temp directory that I need to clean out every once and a while. The code works but not to my satisfaction. I would like to be able to send all errors to an error.txt file. How do you send output of a 'warn' or 'die' to a file?

I am also having a problem with the program trying to delete the actual 'foo' directory. The last line of my log file says:

'Directory: . - has been deleted.'
And I get an error message on the console that says:
'Cannot delete directory: . (Permission denied) at clear_transfer.pl line 58.'

I tried to use the Cookbooks example of ignoring '.' in a directory match, but then the program ignored all the directories that were over 30 days (obviously because the program was in whatever subdirectory it was trying to delete and wouldn't because that would match '.').

Those are the two things bothering me at the moment. If you see something else, please don't hesitate to point it out.

Thanks,
djw
#!/usr/bin/perl -w use strict; # uuuuussseeee thiiiissss use File::stat; # need this for ctime (creation time using sec +onds since epoch) use File::Find qw(finddepth); # need this for recursive directory my($create_date, $current_date, $time_month, $month_old); my($dir,$dirs,$logfile,$logfile2); ############################## # good little purl h4x0r's # # define their variable's # ############################## $_ = *File::Find::name; $month_old = 2592000; # set this to seconds. 2592000 = 30 d +ays $current_date = time; # gives current time since the epoch +in seconds $dir = 'c:\temp\foo'; $logfile = 'c:\temp\log.txt'; $logfile2 = 'd:\temp\log_keep.txt'; ######################################## # The switch is flipped! # # using File:Find:finddepth # # to step through all subdirectories # # using my function to test and delete # ######################################## finddepth \&deletion, $dir; ############################################# # in my sub I start with files then # # directories. Each creation date is # # tested before the nested if statements # # are run so we can see if the data needs # # deletion or not (oder than 30 days). # # I also log all deletion and non deletion # # activity # ############################################# sub deletion { if (-f) { $create_date = stat($_)->ctime; if ($create_date < ($current_date - $month_old)) { unlink ($_) or warn "Cannot delete file: $_ ($!)"; open(LOG, ">>$logfile") or warn "discarding logfile output +\n"; print LOG "FIle: $_ - has been deleted.\n"; close (LOG) or warn "Can't close: $!"; } else { open(LOG, ">>$logfile2") or warn "discarding logfile outpu +t\n"; print LOG "The file $_ is newer than 30 days.\n"; close(LOG) or warn "Can't close: $!"; } } else { $create_date = stat($_)->ctime; if ($create_date < ($current_date - $month_old)) { rmdir ($_) or warn "Cannot delete directory: $_ ($!)"; open(LOG, ">>$logfile") or warn "discarding logfile output +\n"; print LOG "Directory: $_ - has been deleted.\n"; close (LOG) or warn "Can't close: $!"; } else { open(LOG, ">>$logfile2") or warn "discarding logfile outpu +t\n"; print LOG "The dir $_ is newer than 30 days.\n"; close(LOG) or warn "Can't close: $!"; } } }

In reply to A couple of problems.... by djw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.