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

What am I doing wrong ... as no files are being returned. I have tried several different ways as in the following code. Also replacing '/' with '.' and getting the same result. Thanks in Advance.

my $curdir = $sftp->cwd; print"\ncurdir = $curdir"; $envment = "b"; $pat1 = $envment . "REP_*.dat"; $pat2 = $envment . "REQ_*.report"; $pat3 = $envment . "REQ_*.report.*"; $pat4 = $envment . "REQ_*.req.copied"; $ls1 = $sftp->ls('/', names_only => 1, wanted=>qr/$pat1/); $ls2 = $sftp->ls('/', names_only => 1, wanted=>qr/$pat2/); $ls3 = $sftp->ls('/', names_only => 1, wanted=>qr/$pat3/); $ls4 = $sftp->ls('/', names_only => 1, wanted=>qr/$pat4/); print "\nH1. Attempt to list files "; foreach $file ( @{$ls1}, @{$ls2}, @{$ls3}, @{$ls4} ) { print "\nH1. $file "; } @ls1 = @{ $sftp->ls('/', names_only => 1, wanted=>qr/$pat1/) }; @ls1 = @{ $sftp->ls('/', names_only => 1, wanted=>qr/$pat2/) }; @ls1 = @{ $sftp->ls('/', names_only => 1, wanted=>qr/$pat3/) }; @ls1 = @{ $sftp->ls('/', names_only => 1, wanted=>qr/$pat4/) }; print "\nH2. Attempt to list files "; foreach $file ( @{$ls1}, @{$ls2}, @{$ls3}, @{$ls4} ) { print "\nH2. $file "; } ##my $files = $sftp->glob("/b*.dat", names_only => 1); ##print "\n11. $files"; $file{pattern1} = $envment . "REP_*.dat"; $file{pattern2} = $envment . "RPi_*.dat"; while ( ( $key, $pattern ) = each ( %file ) ) { print "\nKey = $key, Pattern = $pattern"; $ls = $sftp->ls('.', names_only => 1, wanted=>qr/$pattern/); print "\nH3. Attempt to list files "; foreach $file ( @{$ls} ) { print "\nH3. File = $file"; } ## end foreach $file ( @files ) } ## end while

Snippet of result from Debug

H1. Attempt to list files #26095 1456788603.00000 _rel2abs: '/' --> '/'

#26095 1456788603.00000 _rel2abs: '/' --> '/'

#26095 1456788603.00000 _queue_msg: queueing msg len: 10, code:11, id:29 ... 1

00 00 00 0a 0b 00 00 00 1d 00 00 00 01 2f | ............./

#26095 1456788603.00000 _get_msg: waiting for message... 1

#26095 1456788603.00000 _do_io: _do_io connected: 1

#26095 1456788603.00000 _do_io: _do_io select(-,-,-, undef)

#26095 1456788603.00000 _do_io: _do_io write queue: 14, syswrite: 14, max: 65536, $!:

00 00 00 0a 0b 00 00 00 1d 00 00 00 01 2f | ............./

#26095 1456788603.00000 _do_io: _do_io select(-,-,-, undef)

#26095 1456788603.00000 _do_io: _do_io read sysread: 17, total read: 17, $!:

00 00 00 0d 66 00 00 00 1d 00 00 00 04 00 00 00 00 | ....f............

#26095 1456788603.00000 _get_msg: got it!, len:13, code:102, id:29, status: -

66 00 00 00 1d 00 00 00 04 00 00 00 00 | f............

#26095 1456788603.00000 opendir: new remote dir '/' open, rid:

00 00 00 00 | ....

#26095 1456788603.00000 _queue_msg: queueing msg len: 13, code:12, id:30 ... 1

00 00 00 0d 0c 00 00 00 1e 00 00 00 04 00 00 00 00 | .................

#26095 1456788603.00000 _get_msg: waiting for message... 1

#26095 1456788603.00000 _do_io: _do_io connected: 1

#26095 1456788603.00000 _do_io: _do_io select(-,-,-, undef)

#26095 1456788603.00000 _do_io: _do_io write queue: 17, syswrite: 17, max: 65536, $!:

00 00 00 0d 0c 00 00 00 1e 00 00 00 04 00 00 00 00 | .................

#26095 1456788603.00000 _do_io: _do_io select(-,-,-, undef)

#26095 1456788603.00000 _do_io: _do_io read sysread: 14587, total read: 14587, $!:

00 00 38 f7 68 00 00 00 1e 00 00 00 64 00 00 00 01 2e 00 00 00 39 64 72 77 78 72 77 78 72 77 78 | ..8.h.......d........9drwxrwxrwx

20 20 20 20 37 20 72 6f 6f 74 20 20 20 20 20 6f 74 68 65 72 20 20 20 20 20 20 39 34 32 30 38 30 | 7 root other 942080

20 46 65 62 20 32 39 20 31 38 3a 30 37 20 2e 00 00 00 0f 00 00 00 00 00 0e 60 00 00 00 00 00 00 | Feb 29 18:07 ...........`......

00 00 01 00 00 41 ff 56 d4 d0 56 56 d4 cf 1b 00 00 00 02 2e 2e 00 00 00 3a 64 72 77 78 72 77 78 | .....A.V..VV............:drwxrwx

72 77 78 20 20 20 20 37 20 72 6f 6f 74 20 20 20 20 20 6f 74 68 65 72 20 20 20 20 20 20 39 34 32 | rwx 7 root other 942

30 38 30 20 46 65 62 20 32 39 20 31 38 3a 30 37 20 2e 2e 00 00 00 0f 00 00 00 00 00 0e 60 00 00 | 080 Feb 29 18:07 ............`..

00 00 00 00 00 00 01 00 00 41 ff 56 d4 d0 56 56 d4 cf 1b 00 00 00 03 62 69 6e 00 00 00 3b 64 2d | .........A.V..VV.......bin...;d-

2d 78 2d 2d 78 2d 2d 78 20 20 20 20 32 20 72 6f 6f 74 20 20 20 20 20 6f 74 68 65 72 20 20 20 20 | -x--x--x 2 root other

20 20 20 20 34 30 39 36 20 4e 6f 76 20 20 35 20 20 32 30 30 34 20 62 69 6e 00 00 00 0f 00 00 00 | 4096 Nov 5 2004 bin.......

00 00 00 10 00 00 00 00 00 00 00 00 01 00 00 40 49 54 cd aa 42 41 8b d3 6b 00 00 00 03 65 74 63 | ...............@IT..BA..k....etc

00 00 00 3b 64 72 2d 2d 72 2d 2d 72 2d 2d 20 20 20 20 32 20 72 6f 6f 74 20 20 20 20 20 6f 74 68 | ...;dr--r--r-- 2 root oth

65 72 20 20 20 20 20 20 20 20 34 30 39 36 20 4e 6f 76 20 20 35 20 20 32 30 30 34 20 65 74 63 00 | er 4096 Nov 5 2004 etc.

00 00 0f 00 00 00 00 00 00 10 00 00 00 00 00 00 00 00 01 00 00 41 24 56 d4 13 87 41 8b d3 6b 00 | .....................A$V...A..k.

00 00 07 6e 6f 74 69 63 65 73 00 00 00 3f 64 72 77 78 72 77 78 72 77 78 20 20 20 20 32 20 6f 70 | ...notices...?drwxrwxrwx 2 op

Replies are listed 'Best First'.
Re: SFTP Foreign Issues using $sftp->ls
by salva (Canon) on Mar 01, 2016 at 07:22 UTC
    You are looking for files on the root directory (/). It is unlikely they are there!

      They are there ...

      sftp> pwd

      Remote working directory: /

      sftp> ls -l bREQ*.req.copied

      -rw-r--r-- 0 2960 100 1253 Feb 29 11:52 bREQ_20160229115227.req.copied

      -rw-r--r-- 0 2960 100 1240 Feb 29 11:52 bREQ_20160229115229.req.copied

      sftp>

      They are there ...

      sftp> pwd

      Remote working directory:

      sftp> ls -l bREQ*.req.copied

      -rw-r--r-- 0 2960 100 1253 Feb 29 11:52 bREQ_20160229115227.req.copied

      -rw-r--r-- 0 2960 100 1240 Feb 29 11:52 bREQ_20160229115229.req.copied

      sftp>

Re: SFTP Foreign Issues using $sftp->ls
by Lotus1 (Vicar) on Mar 01, 2016 at 16:40 UTC
    print "\nH1. Attempt to list files "; foreach $file ( @{$ls1}, @{$ls2}, @{$ls3}, @{$ls4} ) { print "\nH1. $file "; }

    $sftp->ls() returns a reference to an array of hashes. You are printing the reference to the hash. Try the example given in the documentation for Net::SFTP::Foreign and let us know how that works. It is printing the 'filename' value in the returned hash. If you leave out the directory name it will list the contents of the current working directory.

    my $ls = $sftp->ls('/home/foo') or die "unable to retrieve directory: ".$sftp->error; print "$_->{filename}\n" for (@$ls);

    Update: Also I just noticed you are using glob style wildcards inside a regular expression for your ls function call. That won't work as you expect for a regular expression.

    $pat4 = $envment . "REQ_*.req.copied"; ... $ls4 = $sftp->ls('/', names_only => 1, wanted=>qr/$pat4/);

    In your $pat4 regular expression it is looking for 0 or more copies of '_' followed by any single character ( '.' ) followed by req then any single character, etc. Why not start with a small test of just printing all files then try to get the regular expression working.

      Actually the module has a glob method:
      my @files = $sftp->glob($envment."{REP_*.dat,REQ_*.report,REQ_*.report +.*,REQ_*.req.copied}", names_only => 1);
      Every call to glob retrieves the full remote directory so it is better to use a single glob covering all the file names if possible.
        Actually the module has a glob method:

        Yes I know but the title of the OP was about ls() and the OP is using glob style wildcards in a regex in the ls(). I don't feel like rewriting a long convoluted script when the OP needs to try a few simple things and get something working.

        And the glob method returns the same style reference to an array of hashes. The OP was incorrectly dereferencing the data structure returned from ls which I pointed out and you did not. Do you think they would do it correctly for glob or find or something else?

        This works as I expected from my previous attempts. Thanks.

        my @files = $sftp->glob($envment."{REP_*.dat,REQ_*.report,REQ_*.repor +t +.*,REQ_*.req.copied}", names_only => 1);