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

Hi Monks, I have two questions regarding FTP implementation using Perl. - I want to know how i can search specific filenames before retreiving them. - I want to get the files into a specific directory.
$ftp=Net::FTP->new($host,Timeout=>240) $ftp->login("login","password") $ftp->cwd($directory) # Here can i grep by the filename (same search for all files named Mon +k @files=$ftp->dir for each (@files) print "$_\n"; # Need to get the file here. I can print out the files }
Thanks for your help.

Replies are listed 'Best First'.
Re: FTP Grep?
by bart (Canon) on Aug 06, 2009 at 09:59 UTC
    @files=$ftp->dir;
    You're already getting the list of files here. So all you need to do is see loop through the list, see if the file matches your test, and if not, just skip to the next file.
    foreach (@files) { next unless /Monk/; print "$_\n"; # do something with $ftp->get... I presume }

      Why don't you use grep here?

      my @files=grep /Monk/,$ftp->dir();

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Sorry for the late reply, but thanks all of you Guys. You are brilliant!! ^-^ :)
Re: FTP Grep?
by BrowserUk (Patriarch) on Aug 06, 2009 at 06:54 UTC

    The FTP standard doesn't support inspecting the contents of remote files, It is purely for File Transfer as the name suggests.

    You'd need a remote logon (eg. ssh) or a custom app on the remote machine.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I am not looking to search inside the files. I am looking to get files of a certain name. Is that not possible?
        I am not looking to search inside the files.

        ... then describing your requirements as "grep" is misleading.

        Typically, you would use the FTP command mget monk*, but Net::FTP doesn't seem to support that (although it is not listed as unsupported either?).

        If all the files you want will be in a single directory, you might do an ls or dir command to retrieve the file list, then filter that yourself and issue get commands for each.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.