Home > Archive > PERL Beginners > August 2007 > Get names of files ONLY using module Net::SFTP::Foreign
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Get names of files ONLY using module Net::SFTP::Foreign
|
|
| Marian Bednar 2007-08-24, 7:01 pm |
| Hi all,
I am a newbie in this forum and Perl too ;-)
I am trying writing script transfering files using module
Net::SFTP::Foreign.
I need to retrieve from remote directory only names of files (not
directories, links,etc.).
Something like this doesn't work:
--------------
use strict;
use warnings;
use Net::SFTP::Foreign;
my $remote_dir = "/tmp";
my $sftp = Net::SFTP::Foreign->new("host");
$sftp->error and print "SSH connection failed: " . $sftp->error . "\n";
$sftp->setcwd($remote_dir);
my $DH = $sftp->opendir($remote_dir);
while ($_ = $sftp->readdir($DH)) {
print "$_->{filename} \n" if -f $_->{filename};
}
---------------
Script above write out nothing, but if last line is this
print "$_->{filename} \n";
It writes out names of files, directories, but I need only names of files.
How can I get it easy?
Thanks.
Marian
| |
| Stephen Kratzer 2007-08-24, 7:01 pm |
| On Friday 24 August 2007 08:57:26 Marian Bednar wrote:
> Hi all,
>
>
> I am a newbie in this forum and Perl too ;-)
>
> I am trying writing script transfering files using module
> Net::SFTP::Foreign.
>
> I need to retrieve from remote directory only names of files (not
> directories, links,etc.).
>
> Something like this doesn't work:
>
> --------------
> use strict;
> use warnings;
> use Net::SFTP::Foreign;
>
> my $remote_dir = "/tmp";
> my $sftp = Net::SFTP::Foreign->new("host");
> $sftp->error and print "SSH connection failed: " . $sftp->error . "\n";
>
> $sftp->setcwd($remote_dir);
> my $DH = $sftp->opendir($remote_dir);
> while ($_ = $sftp->readdir($DH)) {
> print "$_->{filename} \n" if -f $_->{filename};
> }
> ---------------
>
> Script above write out nothing, but if last line is this
>
> print "$_->{filename} \n";
>
> It writes out names of files, directories, but I need only names of files.
> How can I get it easy?
>
> Thanks.
>
> Marian
You cannot use -f. You'll need to stat the file and check the mode.
| |
| John W. Krahn 2007-08-24, 9:59 pm |
| Stephen Kratzer wrote:
> On Friday 24 August 2007 08:57:26 Marian Bednar wrote:
>
> You cannot use -f. You'll need to stat the file and check the mode.
-f *does* stat the file. The reason that you can't use -f (or stat) is
because $_->{filename} is not on a locally mounted file system.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|
|
|
|
|