Charles Maier wrote:

> Thanks Chris..
> 
> So far everyone has jumped on using dos command... And at some cost. To
> answer your question.. Yes I need to find the time/date on the "newest"
> file in the dir.... But this is not tooo difficult once I know which
> file IS the newest!! I have many directories of hundreds of files.... I
> ultimately want to display to my user that a particular directory has
> new files  as of "today" or "yesterday"... All possible if I could
> easily test and determine which file in a particular directory is the
> "newest".
> 
> I assume there is no simple way to do this.. In perl?? My webserver is
> running on Win xp and I am running Perl 5.8.6

This takes .046/7 seconds for like 500 files (slightly slower if you
do single readdirs instead of into an array) :

use strict;
use warnings;

my $result = Win32::GetTickCount ();
my $dir = shift || '.';
opendir DIR, $dir or die "opendir '$dir': $!";
my @files = readdir DIR;
my $oldest = '';
my $mtime = 0;
my $cnt = 0;
foreach (@files) {
        next if /^.{1,2}$/;
        my @s = stat;
        next if -d _;
        ++$cnt;
        if ($s[9] > $mtime) { # 8=a, 9=m, 10=c
                $oldest = $_;
                $mtime = $s[9];
        }
}
closedir DIR;
print "oldest file is $oldest - ", scalar localtime $mtime, "\n";
my $diff = Win32::GetTickCount ();
printf "%.3f seconds for $cnt files\n", ($diff - $result) / 1000;

__END__

oldest file is test.pl - Mon Jun 20 20:06:22 2005
0.047 seconds for 459 files


This took basically the same amount of time:

use strict;
use warnings;

my $result = Win32::GetTickCount ();
my $dir = shift || '.';
my @files = `$ENV{COMSPEC} /C dir /T:W /O:-D /A:-D /B $dir`;
my $oldest = $files[0];
chomp $oldest;
my $mtime = (stat $oldest)[9];
print "oldest file is $oldest - ", scalar localtime $mtime, "\n";
my $diff = Win32::GetTickCount ();
printf "%.3f seconds\n", ($diff - $result) / 1000;

__END__

So I would just use the first example with readdir and stat since
it's more Perl and more portable.

-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to