Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FTP most recent file - Perl

Status
Not open for further replies.

jodugg

Programmer
Jun 22, 2004
9
US
I am trying to get the latest file from an ftp site that
matches a pattern and is the latest one.

The file name format is: foobar_5279_09-03-2008.log.gz

I really need help with two things: 1) regular expression for file name pattern matching 2) logic to get the most recent file.

Thanks much...
 
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)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top