perl取得目录中最新的文件
2013-04-03 15:59:00 阿炯

本站赞助商链接,请多关照。 ---------------
使用内置函数'stat'来实现

opendir(my $DH, $DIR) or die "Error opening $DIR: $!";
my @files = map { [ stat "$DIR/$_", $_ ] } grep(! /^\.\.?$/, readdir($DH));
closedir($DH);
#按最后修改时间进行排序
sub rev_by_date { $b->[9] <=> $a->[9] }
my @sorted_files = sort rev_by_date @files;

After this, @sorted_files contains the sorted list, where the 0th element is the newest file, and each element itself contains a reference to the results of stat, with the filename itself in the last element:
my @newest = @{$sorted_files[0]};
my $name = pop(@newest);#$newest[13]

The advantage of this is that it's easier to change the sorting method later, if desired.

EDIT: here's an easier-to-read (but longer) version of the directory scan, which also ensures that only plain files are added to the listing:
my @files;
opendir(my $DH, $DIR) or die "Error opening $DIR: $!";
while (defined (my $file = readdir($DH))) {
 my $path = $DIR . '/' . $file;
 next unless (-f $path);           # ignore non-files - automatically does . and ..
 push(@files, [ stat(_), $path ]); # re-uses the stat results from '-f'
}
closedir($DH);
print $files[0][13];
print pop(@{$files[0]});

注意:
在'(my $file=readdir($DH))'时需要用'defined'指令来'防止'其出现错误时的异常处理。
The test for defined() on the result of readdir() is because a file called '0' would cause the loop to fail if you only test for if (my $file=readdir($DH))

---------------
使用(核心)模块'File::stat'
use File::stat;
$dirname = '/root/bin/sys';
$timediff=0;
opendir DIR, "$dirname";
while (defined ($file = readdir(DIR))){
 if($file ne "." && $file ne ".."){
  $diff=time()-stat("$dirname/$file")->mtime;
  if($timediff == 0){
   ($timediff,$newest)=($diff,$file);
  }
  if($diff<$timediff){
   ($timediff,$newest)=($diff,$file);
  }
 }
}
print $newest,"\n";

可以得到最新的文件名称。

---------------
使用'File::DirList'模块
use File::DirList;
my @list = File::DirList::list('.', 'M');

---------------
使用'File::Spec'模块
You don't need to keep all of the modification times and filenames in a list, and you probably shouldn't. All you need to do is look at one file and see if it's older than the oldest you've previously seen:

{opendir my $dh, $dir or die "Could not open $dir: $!";
my( $newest_name, $newest_time );
while( defined( my $file = readdir( $dh ) ) ) {
 my $path = File::Spec->catfile( $dir, $file );
 next if -d $path; # skip directories, or anything else you like
 ( $newest_name, $newest_time ) = ( $file, -M _ )
 if( ! defined $newest_time or -M $path < $newest_time );
}
print "Newest file is $newest_name\n";

}

---------------

在ftp服务器里查找最新文件
Are you using Net::FTP? You can get the modification times with that module, then look for the file with the newest modification time. For example:

use Net::FTP;
my $ftp = Net::FTP->new('ftp.cpan.org', Debug => 1)
    or die "Couldn't connect\n";
$ftp->login('anonymous','')
    or die "Couldn't log in\n";
my $newestfile;
my $newesttime = 0;
foreach my $file ($ftp->ls){
  my $mdtm = $ftp->mdtm($file)
      or next;
  if ($mdtm > $newesttime){
    $newestfile = $file;
    $newesttime = $mdtm;
  }
}
print "Getting file '$file'\n";
$ftp->get($newestfile);

---------------
可借助系统指令(shell's ls command)来实现:
@list = `ls -t`;
$newest = $list[0];
my $newest_file = `bash -c 'ls -t | head -1'`;
ls -lt | grep ^- | head -1