Perl:文件和目录测试不适用于Net :: FTP

时间:2016-01-19 18:02:41

标签: perl net-ftp

我想测试我用ftp下载的文件和目录,如果它们是文件或目录的话。使用下面的代码,我总是得到else语句( 2,4,6 $x(文件或目录)。这段代码有什么问题?

use Net::FTP;

my $host = "whatever";
my $user = "whatever";
my $password = "whatever";

my $f = Net::FTP->new($host) or die "Can't open $host\n";
$f->login($user, $password) or die "Can't log $user in\n";

# grep all folder of top level
my @content = $f->ls;

# remove . and ..
@content = grep ! /^\.+$/, @content;

foreach my $x (@content) {

    print "\n$x: ";
    if ( -f $x ) {print " ----> (1)";} else {print " ----> (2)";}
    if ( -d $x ) {print " ----> (3)";} else {print " ----> (4)";}
    if ( -e $x ) {print " ----> (5)";} else {print " ----> (6)";}
}

1 个答案:

答案 0 :(得分:4)

根据我在OP中的评论,configFilePath = os.path.join(getBundlePath(), 'config.txt') 只返回实体名称列表,但不会将它们提取到本地计算机。要将ls个文件与目录一起使用,您需要使用get。这是一个例子:

Net::FTP::Recursive

示例输出:

use warnings;
use strict;

use Net::FTP::Recursive;

my $host = 'localhost';
my $user = 'xx';
my $password = 'xx';

my $f = Net::FTP::Recursive->new($host) or die "Can't open $host\n";
$f->login($user, $password) or die "Can't log $user in\n";

my @content = $f->ls;

@content = grep ! /^\.+$/, @content;

# note the rget() below, not just get. It's recursive

$f->rget($_) for @content;

foreach my $x (@content) {

    print "\n$x: ";
    if ( -f $x ) {print " ----> (1)";} else {print " ----> (2)";}
    if ( -d $x ) {print " ----> (3)";} else {print " ----> (4)";}
    if ( -e $x ) {print " ----> (5)";} else {print " ----> (6)";}
}