in reply to Get latest file based on name
I enforce that all file names in interfaces are ascii-sortable, by making people name them in a schema like $file-yyyymmdd-hhmmss . This makes it fairly trivial to sort them and extract the first or latest file:</c>
my($first) = (sort @files)[0]; my($last) = (sort @files)[-1];
If your files are not named in a sane way (for example German dates are of the form dd.mm.yyyy), you will have to look at sort on how to sort by arbitrary values.
From looking at your code, you might be interested in glob. Also, I often use a regular expression to get at the filename part:
my $file = "/some/file/name.txt"; my $filename; if ($file =~ m!(?:.*/)?([^/])$!) { $filename = $1; } else { die "Weird file name format: '$filename'"; }; print "Got $filename";
|
|---|