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

I am needing to convert an old Perl program from using Net::FTP::Recursive to SFTP. I don't see anything in any documentation or examples on how to just create a file list to a file from the remote server to a local directory and file. In the FTP there was a method called rdir which I used. Thanks Michael Gould
  • Comment on How to get directory listing using Net::SFTP::Recursive

Replies are listed 'Best First'.
Re: How to get directory listing using Net::SFTP::Recursive
by choroba (Cardinal) on Oct 20, 2016 at 14:22 UTC
    I don't understand what you mean by "create a file list to a file from the remote server to a local directory and file", but searching the documentation of Net::SFTP::Foreign (which I'd recommend for SFTP) for the string "recurs" yields lots of matches. Maybe be more concrete, show what you need to do and how you did it via FTP?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: How to get directory listing using Net::SFTP::Recursive
by stevieb (Canon) on Oct 20, 2016 at 14:23 UTC

    If I understand correctly, you want to recursively list all files/directories within a location on a remote server, then save the results to a local file.

    This code, using Net::SFTP::Foreign's find() method (even though I'm using 'localhost' as the remote), does just that (using my scratch working directory underneath the SFTP root, as my whole home dir would take far too much time to test):

    use warnings; use strict; use Net::SFTP::Foreign; my $host = 'localhost'; my $list_file = 'files.txt'; my $sftp = Net::SFTP::Foreign->new($host); # look in my scratch working directory my @list = $sftp->find('scratch'); open my $wfh, '>', $list_file or die $!; print $wfh "$_->{filename}\n" for @list;

    My local output file contains:

    scratch scratch/sftp.pl scratch/dt.pl scratch/time.pl scratch/auth.pl scratch/or.pl scratch/test scratch/test/a.txt