Klaas has asked for the wisdom of the Perl Monks concerning the following question:
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!EDIT: working code, if anyone is interested: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;
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: New to Perl: Finding files on FTP
by muppetjones (Novice) on Mar 14, 2012 at 17:13 UTC | |
by Klaas (Initiate) on Mar 14, 2012 at 18:03 UTC | |
by runrig (Abbot) on Mar 14, 2012 at 20:29 UTC | |
by Klaas (Initiate) on Mar 15, 2012 at 10:58 UTC | |
by muppetjones (Novice) on Mar 14, 2012 at 21:00 UTC |