#!/usr/bin/perl
use strict;
my ($MYDIR, $mtime, $filename,%filearray, $key);
my $LOGDIR="/mytest/logs";
sub numerically { $filearray{$a} <=> $filearray{$b}; }
# Read all the file names in to an array
opendir $MYDIR, $LOGDIR or die "Unable to open file: $!\n";
my @allfiles = readdir $MYDIR;
closedir $MYDIR;
# For each file, get its modification time and store
print "Unsorted\n";
for (@allfiles) {
next if $_ eq "." || $_ eq "..";
$filename = $_;
$mtime = (stat("/mytest/logs/" . $filename))[9];
printf "\t$mtime\t\t$filename\n";
# populate a hash called %filearray with a
# key of the file name and a value of the mtime
$filearray{$filename} = $mtime;
}
print "Sorted\n";
foreach $key (sort numerically ( keys(%filearray))) {
print "\t$filearray{$key}\t\t$key\n";
}
Just somewhere to keep my notes while I'm playing.
Tuesday, April 23, 2013
Displaying files in date order
Here is an example of a program that reads all the files directory and displays them in date order.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment