Neel
Neel

Reputation: 97

How can download via FTP all files with a current date in their name?

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?

Upvotes: 1

Views: 3862

Answers (2)

brian d foy
brian d foy

Reputation: 132914

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

Upvotes: 3

Beau Simensen
Beau Simensen

Reputation: 4578

I do not think regexes work like that. Though it has been awhile since I did Perl, so I could be way off there. :)

Does your $ftp object have access to an mget() method? If so, maybe try this?

$ftp->mget("IDY*.$year$mon$mday*.axf") or warn $ftp->message;

Upvotes: 1

Related Questions