Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Please help! I have a folder containing files with fixed format filename: Load_BBBB where BBBB is the Batch number. I would need to read the last file in the folder to extract out the latest Batch number. How do I get this file? Thanks a lot. Lily

Replies are listed 'Best First'.
Re: File search
by TGI (Parson) on Feb 06, 2002 at 21:06 UTC

    Well, I would use opendir to open a directory handle, and readdir to read the results into a list. Then I'd push the list through some sort of loop, like foreach or while and strip out the numbers from the names (of course you could also use map to process the whole list all at once). Once I had the numbers in a list by themselves, I'd sort the list and pull out the largest either by getting the first or last element of the result. If I had a very large number of files and sort was taking too long, I'd use a loop, probably foreach, and store the largest value I had seen at any point in a variable, so that when I finished the list, I'd have the largest. I'm just lazy and the sort approach would take less typing, even though it is much slower.


    TGI says moo

Re: File search
by rjray (Chaplain) on Feb 06, 2002 at 23:19 UTC

    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

Re: File search
by dsb (Chaplain) on Feb 06, 2002 at 20:48 UTC

    Perhaps offer some code you have already tried so we can help you, or at least tell us what you have tried.

    People around here aren't exactly fond of doing other people's work for them. ;)

    Amel - f.k.a. - kel

Re: File search
by seattlejohn (Deacon) on Feb 07, 2002 at 00:01 UTC
    It's not entirely clear from your note whether you want the highest batch number or the most recently modified file. Either way, you could just use Perl to wrap an OS command something like this:
    my $latest_batch_filename = (`ls Load_* -t -1`)[0]; $latest_batch_filename =~ m/^Load_(\d+)$/; my $latest_batch_number = $1;

    In this case, `ls Load_* -t -1` will grab a list of files sorted by time. (If you're looking for the highest batch number and that's not correlated with the timestamps on files, you'll have to sort differently.) By extracting the zeroth element of the resulting list, you get the most recently modified one. The regex extracts the actual batch number from that filename.

    This assumes you're using a Unix-ish OS; in DOS/Windows you'd go with something more like `dir Load_* /o:-d/b`. Anyway, you can try various commands in your shell until you get the results you want, then use backticks to slurp that into your Perl script.