Ok, here is a suggestion (not tested with data):
First, open the files and directories that you need so that this is all in one place at the beginning. I mean if this was a huge calculation monster, you wouldn't want it to run for an hour only to find out that you couldn't open the file to write the data to! That's a style thing for future reference.
File and directory handles will close when the program exits. This program will run in far less than one second and is just ~20 lines. Don't worry about close($x).
#!/usr/bin/perl -w
use strict;
my $logfile = "script_logs/30DyDlt-LOG.txt";
open (LOGFILE, ">>$logfile") or die "Cannot open logfile $logfile for
+append $!";
my $dir = 'oven_web/iportal/cgi-bin/uploads';
opendir(DIR,$dir) || die "Can't open directory $dir $!";
my @files = grep{-f "$dir/$_"} readdir(DIR);
my $current_date_time = time(); #this is seconds +- epoch value!
my $thirty_days = 60 * 60 * 24 *30; #num seconds in 30 days
foreach my $file(@files)
{
my $file_time = (stat("$dir/$file"))[9];
if ( $file_time < ($current_date_time - $thirty_days) )
{
print "Deleting $dir/$file...\n"; #for the user on terminal
unlink("$dir/$file") or die "unable to unlink file $file in $dir $
+!";
print LOGFILE "$dir/$file deleted\n";
}
}
Update:
could be better: have fun!
foreach $file (readdir D)
{
if ((-M "$dir/$file" > 30) && !unlink("$dir/$file"))
{
print "Oh no!!!!!\n";
}
# else it worked ###
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.