3

我通过 Perl 中的 NET::SSH2 包使用 SFTP。使用 opendir 函数列出文件夹中的文件效果很好。

我想让文件按时间顺序排列,最后修改最新的文件。这可能吗?怎么做?

4

2 回答 2

2

方法返回的哈希值Net::SSH2::Dir::read和调用mtime的条目指示条目被修改的时间。使用它对条目进行排序:

my @e;
my $dir = $sftp->opendir($dir);

while (my $e = $dir->read) {
  push @e, $e;
}

@e = sort { $a->{mtime} <=> $b->{mtime} } @e;
print "$_->{name}\n" for @e;
于 2012-10-09T09:02:58.370 回答
1

你不能直接执行 using exec('ls -t')through plainSSH吗?(没有SFTP)。

就像是:

my $chan = $ssh2->channel();
$chan->exec('ls -t'); # executed in the dir you would like to get the files sorted from
于 2012-10-08T10:48:01.227 回答