R – How to download via FTP all files with a current date in their name

ftpperl

I have a file format which is similar to "IDY03101.200901110500.axf". I have about 25 similar files residing in an ftp repository and want to download all those similar files only for the current date. I have used the following code, which is not working and believe the regular expression is incorrect.

my @ymb_date = "IDY.*\.$year$mon$mday????\.axf";

foreach my $file ( @ymb_date) 
{

 print STDOUT "Getting file: $file\n";

 $ftp->get($file) or warn $ftp->message;

}

Any help appreciated.

EDIT:

I need all the current days file.
using ls -lt | grep "Jan 13" works in the UNIX box but not in the script
What could be a vaild regex in this scenario?

Best Solution

It doesn't look like you're using any regular expression. You're trying to use the literal pattern as the filename to download.

Perhaps you want to use the ls method of Net::FTP to get the list of files then filter them.

foreach my $file ( $ftp->ls ) {
    next unless $file =~ m/$regex/;
    $ftp->get($file);
    }

You might also like the answers that talk about implementing mget for "Net::FTP" at Perlmonks.

Also, I think you want the regex that finds four digits after the date. In Perl, you could write that as \d{4}. The ? is a quantifier in Perl, so four of them in a row don't work.

IDY.*\.$year$mon$mday\d{4}\.axf
Related Question