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

I am a Newbie using Net::FTP to connect to a ftp site. I am trying to recurse throgh the site and pick up the listings, DIR's or FILE's. I know how to write a recursive program for a file system using (-d for dirs) and (-f for file) tests therefor defining the logic... I have tried for net::ftp but there are no such tests!!! :-( any ideas???

Replies are listed 'Best First'.
Re: Recursion Over FTP
by Preceptor (Deacon) on Sep 27, 2002 at 14:12 UTC
    Unfortunately -f and -d won't work on an FTP server, so Net::FTP isn't going to help.
    It should be possible, but you'll need to send the remote server 'pwd' and 'list' requests in order to figure out the directory structure yourself (which you should be able to do by just looking at the file permissions, and seeing it it's prefixed by a '^d' or not).
    And whilst I know it's not perlish, I suggest taking a look at wget if you are trying to recurse through an ftp site.
    HTH
    --
    It's not pessimism if there is a worse option, it's not paranoia when they are and it's not cynicism when you're right.
Re: Recursion Over FTP
by Snuggle (Friar) on Sep 27, 2002 at 14:58 UTC
    Well not to do it all for you but try something like this, it makes a webpage that shows the contents of a FTP site with links that work:
    #! /usr/bin/perl -w use strict; use Net::FTP; my $ftp = Net::FTP->new("Address to ftp site); print "Content-type: text/html\n\n"; print "<html><title>FTP Contents</title>"; print "<body bgcolor=black text=white link=lime vlink=wheat>"; if($ftp->login("Username", 'password')){ lister(0,"ftp://toplevel/"); } else{ print "FTP: Failed to login"; } $ftp->quit; print "</body></html>"; #----subs---- sub lister{ my $lev = shift; my $path = shift; my @dirs; my @files; my @dirarray = $ftp->ls(); foreach my $item(@dirarray){ if($ftp->cwd($item)){ $ftp->cdup(); push @dirs,$item; } else{ push @files,$item; } } my $buff="&nbsp&nbsp&nbsp&nbsp&nbsp" x $lev; foreach my $dirs(@dirs){ print "<font color=yellow>$buff\|_ $dirs</font><br>"; $ftp->cwd($dirs); lister($lev+1,$path."/".$dirs); } foreach my $file(@files){ print "<font color=lime>$buff\|_ <a href=\"$path/$file +\">$file</a></font><br>"; } $ftp->cdup(); }

    Try looking at this seeing what I am doing. Basically, it checks the dir of a directory, splits the objects into directories and files, and recurses into the directories. Hope it helps.
    Anyway, no drug, not even alcohol, causes the fundamental ills of society. If we're looking for the source of our troubles, we shouldn't test people for drugs, we should test them for stupidity, ignorance, greed and love of power.

    --P. J. O'Rourke
      Awesome This is exactly what i wanted... I learned alot thankz
Re: Recursion Over FTP
by princepawn (Parson) on Sep 28, 2002 at 04:17 UTC