Hi,

I'm very new to perl and somewhat new to programming, and I'd like to use perl to read 10.000s of files from a ftp server and run test on those files on my own computer and save the results. The problem is: I can connect to a fileserver, but I cannot change the subdirectory and make Perl find the files I want for me. My snipped of code I use for finding the files is obviously very flawed, but my head is exploding and I ran out of coffee.

Thank you!
use strict; use warnings; use File::Find; use Net::FTP; use Cwd; my $ftp = Net::FTP->new("ftp.ncbi.nih.gov", Timeout => 30) or die "Can +not connect to server: $@"; my $dir = "/genomes/Bacteria"; $ftp->cwd($dir) or die "Cannot cd to " , $dir; my @directories = $ftp->ls() or die "cannot list any DIRs"; my @files; find( sub { push @files, $File::Find::name if /\.fna$/ }, @directories + ) or die "can't find shit"; for(my $i=0; $i<@files; $i++){ print "$i","x hooray" } $ftp->quit;
EDIT: working code, if anyone is interested:
use warnings; use strict; use Net::FTP; my $ftp = Net::FTP->new("ftp.ncbi.nih.gov", Timeout => 30) or die "Can +not connect to server: $@"; ## login is mandatory, even if no user and password is specified: $ftp->login("anonymous",'-anonymous@') or die "Cannot login ", $ftp->m +essage; my $dir = '/genomes/Bacteria'; ## move to the subdirectory: $ftp->cwd($dir) or die "Cannot cd to " , $ftp->message(); #print "part 1\n"; my @dir_listing = $ftp->ls() or die $ftp->message; ## note: you need to save DIRs as arrays before you can search for spe +cific ## files or anything my @files; for(my $j=0; $j<@dir_listing; $j++){ ##list all the files in subdirs: my @file_list = $ftp-> ls($dir_listing[$j]) or die $ftp->message; ##weed out all the files you want and put them in an array my @found_files = grep(/.fna/, @file_list); push (@files, @found_files); } ##Make a resultfile to print all the files to: my $results = "D:/Genomes/results/filelist.dat"; + open (OUTFILE, '>', $results) or die "Cannot write $!n"; for(my $i=0; $i<@files; $i++){ print OUTFILE "@files[$i]"; } close (OUTFILE); $ftp->quit;

In reply to New to Perl: Finding files on FTP by Klaas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.