in reply to File search
Assuming that you have a potentially large number of items (otherwise, you could just read all of them in with opendir/readdir and sort the list), try this:
$last = -1; opendir(D, "DIRNAME") or die $!; foreach (readdir(D)) { ($num) = $_ =~ /(\d+)/; next unless $num; if ($num > $last) { $pick = $_; $last = $num; } } closedir(D); # Now operate on $pick
As I wrote that, it occurs to me that "latest batch number" may not be "latest batch", if the numbers roll around the 0000-9999 range. If what you really mean is the latest batch as in the newest file, then the loop is mostly the same, only you get to replace the regex with a system call:
$last = -1; opendir(D, "DIRNAME") or die $!; foreach (readdir(D)) { $num = (stat $_)[9]; if ($num > $last) { $pick = $_; $last = $num; } } closedir(D); # Now operate on $pick
--rjray
|
|---|