Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks!
I want to retrieve a list of files that have the extension .txt from an FTP server. My script so far is
:
use Net::FTP; $ftp = Net::FTP->new("SERVER_NAME", Debug => 0) or die "Cannot connect + to the server: $@"; $ftp->login("anonymous",'-anonymous@') or die "Cannot login ", $ftp->m +essage; print "Succesfully connencted to the server...\n"; $ftp->cwd("/downloads/pub") or die "Cannot change working directory ", + $ftp->message; $ftp->get("*.txt") or die "get failed ", $ftp->message; $ftp->quit;
Apparently, it doesn't work if you try to do a unix-like fetch, using *.txt. My problem is that I don't know all the names of the text files, so I must use some with a wild-character use. Any other suggestions?

Replies are listed 'Best First'.
Re: How to fetch all files with a given name from an FTP server
by Anonymous Monk on Sep 01, 2010 at 11:05 UTC
    Use dir/ls to get a list of files, use grep /\.txt$/, to filter that list, then use get() as documented to download those files
Re: How to fetch all files with a given name from an FTP server
by Utilitarian (Vicar) on Sep 01, 2010 at 11:06 UTC
    for my $filename ($ftp->ls()){ $ftp->get($filename) if $filename=~ /\.txt$/; }
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: How to fetch all files with a given name from an FTP server
by marto (Cardinal) on Sep 01, 2010 at 11:13 UTC

    I think you're confusing ftps get (get a file) and mget (get multiple files) commands. Net::FTP doesn't implement mget. See Net::FTP::Recursive which can use a regex for file names.

Re: How to fetch all files with a given name from an FTP server
by Anonymous Monk on Sep 01, 2010 at 11:06 UTC
    Ok, I got it from another discussion:
    $ftp->get($_) for grep /\.txt$/, $ftp->ls;

    Cheers!