Hi Monks!
I an trying to have my code looking in a specific directory or directories for file(s) that are the oldest of all the other files found in that specific directory. After that I need to delete the oldest file found.
My code now can find the age of all the files, but how can I delete de oldest file(s) found inside this directory(ies)?
Thanks for the help!!!
#!/perl/bin/perl -w
use strict;
use warnings;
use Win32;
use Win32::NetResource;
use Time::localtime;
my ($dirh,$dirh_in, @dircontent, $file, @filenames, $age, @outputFiles
+ , $all_dir_path);
$age='';
$dirh_in='';
my $path='C:\\files\\logs';
opendir(DIRH, "$path") || die "Cannot opendir $path: $!";
foreach $dirh (sort readdir(DIRH)) {
$all_dir_path= $path."\\".$dirh;
$all_dir_path=~s/(.*?)\.+//g;
if($all_dir_path ne ""){
chomp($all_dir_path);
opendir $dirh,$all_dir_path or die "Can't open - Check Path
+ - $all_dir_path";
push (@dircontent, map { $_="$all_dir_path/$_"; $_;} rea
+ddir($dirh));
closedir($dirh) or die "Can't close $path";
}
}
closedir(DIRH) or die "Can't close $path";
foreach (@dircontent) {
next unless -f; # ignore all non-normal files.
$age=-M $_;
if($age){
unlink($_) || die "Cannont unlink $_: $!";
print "$_ - $age\n";
}
}
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.