Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fnd latest file in directory -A syntax 2

Status
Not open for further replies.

GrahamBright

Programmer
Oct 31, 2003
65
AT
Hi,

I'm trying to find the latest file in a directory, but am not sure of the syntax for the -A file test

sub latestFile(){
my $BASE = "/apps/omgfs/application/1410_1480/deploy/log/";
opendir(DIR,"$BASE") or die " cannot opendir $BASE : $!";
while (defined($file = readdir(DIR))) {
if ( -A $file ) ???
{
print "$file \n" #print only latest file

}
}
closedir(DIR);
 
Hello Graham,

Opendir() and readdir() return all of the files in a directory. There doesn't appear to be an option to return them in some particular order - which would allow you to pick out the newest file for instance.

What I would do is to process each file that readdir() returns and use stat() to find the date you're interested in. Save the name of the newest file and its date as you go along and compare that date to each file as you process it - saving the name if the current file is newer than the one you have. After you've gone through the list of files you'll have the newest file in the directory.

Like this:
Code:
# always
use strict;
use warnings;

sub latestFile(){

# declare variables
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
my $BASE = "/Perl";
my $file;
my ($newest_file, $newest_file_mtime);

# open the directory for reading
 opendir(DIR,"$BASE") or die " cannot opendir $BASE : $!";

# loop through the list of files using readdir()
 while (defined($file = readdir(DIR))) {

# not interested in the current directory or the parent directory
  next if $file eq '.' or $file eq '..';

# collect the details of that file
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)
    = stat($file);

# if this is first file in list we just save its name and mtime
  unless ($newest_file){ # if first file in dir
   $newest_file = $file;
   $newest_file_mtime = $mtime;
  } else {

# not the first file
# so compare its date stamp with the newest one so far
   if($mtime > $newest_file_mtime){

# current file is newer than newest so far, so save details
    $newest_file_mtime = $mtime;
    $newest_file = $file;
   }
  }
 }
 closedir(DIR);

# when we get here we have the name and date stamp of newest file from the $BASE directory
 print "$newest_file, $newest_file_mtime\n";

}

latestFile();

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Hi Graham,

if you work under Unix/Linux, another solution could be to invoke the shell, as the -t option of ls sorts the files by date descending. As ls returns the total size in the first line, we just need to filter out the second line.

Code:
my $latest_file = `ls -t ~/proj/test | head -2 | tail -1`;

Holger
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top