Perl:文件和目录测试是否不适用于 Net::FTP
Perl: Does file and directory testing not work with 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)";}
}
根据我在 OP 中的评论,ls
只是 returns 实体名称列表,它不会将它们提取到本地机器。要 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)";}
}
示例输出:
a.txt: ----> (1) ----> (4) ----> (5)
dir_a: ----> (2) ----> (3) ----> (5)
我想测试用 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)";}
}
根据我在 OP 中的评论,ls
只是 returns 实体名称列表,它不会将它们提取到本地机器。要 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)";}
}
示例输出:
a.txt: ----> (1) ----> (4) ----> (5)
dir_a: ----> (2) ----> (3) ----> (5)