Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Syntactic Confectionery Delight
 
PerlMonks  

How do I make Net::FTP use wildcards

by rzs96 (Acolyte)
on May 31, 2005 at 16:44 UTC ( [id://462223]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Greetings! I would like to be able to make Net::FTP grab all the files in a directory preferably using wildcards. Do you know how I can go about this?

Here's my code so far

sub ftp_start { my ($ftp, $meetNo); my ($root, $filename); my (@directories); ### THESE 3 WORK FINE: $ftp = Net::FTP->new("atd77.acs.org", Debug => 0) or die "Cannot connect to some.host.name: $@"; $ftp->login("USERNAME",'PASSWORD') or die "Cannot login ", $ftp->message; $ftp->cwd("$REMOTE_DIR") or die "Cannot change working directory ", $ftp->message; # I WANT TO DO THIS: GET ALL FILES IN $REMOTE_DIR and FTP the +m to the local directory. $ftp->get("*.*") or die "get failed ", $ftp->message; $ftp->quit; }
Thanks!
Robert

Replies are listed 'Best First'.
Re: How do I make Net::FTP use wildcards
by fauria (Deacon) on May 31, 2005 at 16:57 UTC
    I dont know if this is optimal, but you can get a list of files using ls, and then download them all:
    use Net::FTP; my $ftp = Net::FTP->new("atd77.acs.org", Debug => 0); $ftp->login("foo",'bar'); my @file_list = $ftp->ls(); foreach (@file_list) { $ftp->get($_); }
      Hey Fauria, thanks! Your solution I believe did the trick! Etm177's solution is probably more sophisticated, and if I run into any further challenges, I may use his mask, or else persuade the boss to install FTP::Recursive.
      This is really an amazing site, I love the quick responses, and know there are cool people here.
      Cheers!
      Robert
Re: How do I make Net::FTP use wildcards
by Adrade (Pilgrim) on May 31, 2005 at 17:00 UTC
    Net::FTP::Recursive should take care of your needs... check it out.

    Best,
      -Adam

    --
    Impossible! The Remonster can only be killed by stabbing him in the heart with the ancient bone saber of Zumakalis!

Re: How do I make Net::FTP use wildcards
by etm117 (Pilgrim) on May 31, 2005 at 17:05 UTC
    To be able to use a mask and not just get all files in the directory (which the above example does), I run a ls command on the directory, then runs the mask through a grep command and then gets those files.

    So I modified your code below to do that (it is untested, but the premise should work fine for you)

    sub ftp_start { my ($ftp, $meetNo); my ($root, $filename); my (@directories); # the Perl regexp mask to get all files my $mask = "."; ### THESE 3 WORK FINE: $ftp = Net::FTP->new("atd77.acs.org", Debug => 0) or die "Cannot connect to some.host.name: $@"; $ftp->login("USERNAME",'PASSWORD') or die "Cannot login ", $ftp->message; $ftp->cwd("$REMOTE_DIR") or die "Cannot change working directory ", $ftp->message; # Here is new code my @files = $ftp->ls("$REMOTE_DIR"); @files = grep { $_ =~ /$mask/ } @files; for my $file ( @files ) { $ftp->get("$file") or die "get failed ", ftp->message; } $ftp->quit; }

    Updated to note that this version works with Perl RegExp as the mask.

Re: How do I make Net::FTP use wildcards
by dReKurCe (Scribe) on Jun 02, 2005 at 16:42 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://462223]
Approved by polettix
Front-paged by grinder
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.