1) Regex for file name pattern matching:
Need more information on naming conventions. I realize that 09-03-2008 is very likely a MDY style date, I can't be 100% sure.
Note that without additional file naming conventions, the following is an assumption on what you're asking for (perhaps not necessarily looking for though)
Code:
$file_name = 'foobar_5279_09-03-2008.log.gz';
($file_head, $MM, $DD, $YYYY, $file_tail) = ($file_name =~ /(.*_)(\d{2})\-(\d{2})\-(\d{4})(\..*)/);
OK, so my regex isn't very elegant, but it'll get what you asked for.
2) Logic for most recent file:
The problem with the above that if you have two files:
foobar_5279_09-03-2008.log.gz
foobar_5280_09-03-2008.log.gz
How can you
guarantee which one is then newest?
If the 5279 and 5280 numbers are leftover from process IDs, all bets are off (one of the systems I work with does this).
If you're using Net::FTP to get your files, there is a mdtm method to determine the modification time of your files. Reviewing the following Net::FTP commands should get your pretty far:
- use Net:FTP
- $ftp = Net:FTP->new($host)
- $ftp->login($name, $passwd)
- $ftp->cwd($source_dir)
- $ftp->ls() # put into an array or hash
- $ftp->mdtm() # loop over above array/hash var and keep track of the newest file.
- $ftp->get($file_identified_in_mdtm_loop)