Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

ftp files from Unix m/c

by Anonymous Monk
on Jul 22, 2002 at 09:33 UTC ( [id://183989]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am trying to get the list of files present in a directory. I used ftp->ls command to do this. this will get all the files in the directory. but the list conains the directory names also. i need to get only the list of files not the directories. If any one knows the answer please send ur reply to murugan_mohan@hotmail.com

Replies are listed 'Best First'.
Re: ftp files from Unix m/c
by amphiplex (Monk) on Jul 22, 2002 at 09:44 UTC
    Hi !

    The simplest method would be to use the cwd method to change the current directory to the one you want to do an ls in.

    BTW: Answers here are generally posted here as a reply node, so that others can see if a question was answered already, correct answers or learn from them.

    Update: Here is some code that might help you:
    use strict; use Net::FTP; my $remote_host = "*****************"; my $remote_user = "*******"; my $remote_password = "*******"; my $remote_dir = "/tmp"; my $ftp = Net::FTP->new($remote_host) or die "error initiating ftp: $! +\n"; $ftp->login($remote_user,$remote_password) or die "couldn't login: $!\ +n"; # force binary transmission of files, generally a good idea $ftp->binary(); my @files = $ftp->ls($remote_dir); print join "\n",@files; print "\n","="x80,"\n"; # now change current working directory and do ls there $ftp->cwd($remote_dir); my @files = $ftp->ls(); print join "\n",@files;

    ---- amphiplex
      I think what the person is asking is how to do a listing, but ignore all of the directory entries in the directory he is reading from.

      Frankly, I don't think this could be done with Net::FTP->ls(), AFAIK, because it doesn't give you any bits to determine between dirs and files, and you certainly can't do a grep ! -d  $_, readdir REMOTE_DIR with the FTP module.

      Perhaps the easiest way would be to use the Net::FTP->dir command. This will give you an ls -l UNIX long directory list with file permissions and stuff. Then you can probably do a  grep ! /^d/, Net::FTP->dir() instead. Of course, then you have to extract the filename from each line from amidst the other info, but that's not hard.

Re: ftp files from Unix m/c
by flounder99 (Friar) on Jul 22, 2002 at 14:02 UTC
    The info returned by using Net::FTP->ls() and ->dir() seems to vary depending on the FTP server. When I do a ->ls() I only get a list of the files and not the directories. If I do a ->dir() I get the results of an ls -lR on the server, showing me all the files in the subdirs also. Sorry but I don't have a simple, portable answer for you.

    --

    flounder

Re: ftp files from Unix m/c
by Anonymous Monk on Jul 23, 2002 at 04:46 UTC
    There is a big problem with different FTP daemons giving different listing formats. I've spent a considerable amount of time on this issue and have researched it back far enough to the root problem which is, there are no set FTPd standard.

    My suggestion is to setup ftpd server type checking at the beginning of your code, then forward through a sub that is specific to the listing format that you will get from that ftpd. Use regular expression, then let hang dry.

    - Simon
    mitosis.com
Re: ftp files from Unix m/c
by rah (Monk) on Jul 24, 2002 at 02:15 UTC
    As other monks have pointed out, the commands supported by the ftp server daemon are the determining factor. You'll need to test based on individual cases. It is not possible to build a completely portable solution if you go beyond the most basic ftp commands.

    You might try using ftp->size(). On servers that support this command, it will return a size in bytes for the file listed (and a 2NN status code), or return an error code (550 Not a plain file). This works on my UNIX/Linux servers.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://183989]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-12-14 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (70 votes). Check out past polls.