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

Hi Monks,

I need to get a list of files present in a directory on a remote server through ftp. I need the list of all the files inside the directory I specify ( files in the subdirectories as well).

So i used a NET FTP Recursive for this. I need to use the rls method. but I keep getting the error about using a filehandle. Can anyone please explain with an example on how to use the rls method.

This is my code snippet
use strict; use warnings; use Net::FTP; use Net::FTP::Recursive; use FileHandle; # after providing the host and user details my $fh = new FileHandle; my $dir = "/home/prod/game/code"; $f->cwd($dir) or die "Can't cwd to $dir\n"; $, = "\n"; select $fh; #Now when i write the rls like this my @files = $f->rls(Filehandle => $fh) or die "??"; #i'm getting a timed out error even for lesser number of files #Now when i write the rls like this w/o declaring any file handler my @files = $f->rls() or die "??"; # i get the error -You must pass a filehandle when using rdelete/rls!

Please help.

Replies are listed 'Best First'.
Re: net ftp recursive - rls and rdir
by Lotus1 (Vicar) on Sep 07, 2012 at 13:01 UTC

    I don't think the filehandle you are passing to rls() is valid. The cpan page for the FileHandle module provides examples that show you have to provide a filename for it to open.

    use FileHandle; $fh = FileHandle->new; if ($fh->open("< file")) { print <$fh>; $fh->close; } $fh = FileHandle->new("> FOO"); if (defined $fh) { print $fh "bar\n"; $fh->close; }

    You need a filehandle for output so use the second version.

    Update: from the documentation for Net::FTP::Recursive:

    All of the methods should return false ('') if they are successful, and a true value if unsuccessful.

    The rls method will write the filenames into the filehandle. The code you showed expects the files to be returned as an array.

    my @files = $f->rls(Filehandle => $fh) or die "??";
Re: net ftp recursive - rls and rdir
by Mr. Muskrat (Canon) on Sep 07, 2012 at 13:52 UTC

    In addition to the filehandle issue I thought I'd point out a few other things.

    Are you using Net::FTP or Net::FTP::Recursive? You've used both but since you didn't show how you used the constructor, we don't know. Net::FTP doesn't support rls.

    Also, in all Net::FTP and Net::FTP::Recursive calls after the constructor you should be using $ftp->message (or in your case $f->message) as a part of your die messages as documented in Net::FTP.

Re: net ftp recursive - rls and rdir
by philiprbrenan (Monk) on Sep 07, 2012 at 13:24 UTC

    Please turn on debugging:

    my $f = Net::FTP->new("ftp.xxx.com", Debug=>1);

    and post the messages that you get so that it is easier to help you. Are you certain that the rls() function is supported by your FTP server? Quite often these kinds of capabilities are turned off for security reasons.

Re: net ftp recursive - rls and rdir
by poori (Initiate) on Sep 10, 2012 at 07:25 UTC
    Thanks guys.

    What i'm looking for is this - I have a list of filenames in my hand. I know they are present in the Unix Sever. I have the name of the parent directory - "/home/prod/game/code". the files may be directly present under this, or in any of the multiple subdirectories(which inturn has many directories under) under it. I do not have the names of all the subdirectories.

    So I need to run a search in this 'code' direcory I have and FTP only the files in the list to my local.

    As of now, I’m only able to retrieve the files we need only after navigating to the correct directory in which it resides(by hard coding the directory name in the code)

    The method i tried above looks complicated. Can you please let me know some solution to get only the desired files.

    Thank You!

      It isn't complicated if you add functionality incrementally. Have you gotten the code you have working with what we gave you so far? That will give you a list of the files in a text file. Then grep that list for the ones you want. Then you just need a function to ftp each file that grep returns.

        Hi Lotus,

        Yes I got the list of files in a text file. but the size of the file is 70 MB. Is there any way we can ignore specific files. I want to ignore .log files while doing rls. Will a OmitAll work?

        And regarding grep, I'm writing this Perl script in a windows system which FTPs files from Unix. So how do i give a grep function into the Perl file. Do I have to use any other package.

        This is a snippet of my output

        /home/prod/test/code

        /

        RxD_CLM_bteq_TEMP_LOAD_V20.sh

        /home/prod/test/code/BABW/logs

        /

        Agent_Totals_CO_Merced_FTP_20110122033155.log