in reply to Re: Get newest file
in thread Get newest file
That will stat all files more than once (remember, sort is N*log(N)), and will, particularly for large directories, break down horribly in performance. The least you can do here is a Schwartzian Transform or variant thereof.
Also remember that globbing for * will miss any files starting with a dot - if that is of significance.
I'd do something like this:
But that still doesn't address the recursive nature of the original poster's task. Zaxo makes the right points.my ($age, $name) = (0, ""); opendir DIR, $directory; while(my $curname = readdir DIR) { my $curage = (stat "$directory/$curname")[9]; next unless -f _; ($name, $age) = ($curname, $curage) if $curage > $age; } closedir DIR;
Makeshifts last the longest.
|
|---|