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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pulling the most recent file by creation date?

Status
Not open for further replies.

calabama

Programmer
Feb 19, 2001
180
US
Cheers everyone,

I am working on a script that fiqures out wheter or not to update and what text to update on a web page. I thought this would be simple bu it is not I am having problems.

The script iterates through a directory storing the real filenames in a variable named @found_files.
T I thought the best way was to compare the seconds from the stat()0 function. But I cant seem to even get that to work. I keep getting hexadecimal values.

Code:
@filenames ="fileone.shtml,filetwo.shtml"; 

foreach my $file (@filenames) {
next if $file eq'.';
next if $file eq'..';
$X++;
push @found_files, $file;
};

foreach my $item (@found_files){
@tmp = localtime($item);
($seconds, $minutes, $hours, $day_of_month, $month, $year, $wday, $yday, $isdst) = localtime($tmp[0]);

print &quot;The file is $item  $seconds<BR>&quot;;
};
In the begining
Let us first assume that there was nothing to begin with.
 
stat returns a 13 element array:
Code:
     stat    Returns a 13-element list giving the status info for
             a file, either the file opened via FILEHANDLE, or
             named by EXPR.  If EXPR is omitted, it stats $_.
             Returns a null list if the stat fails.  Typically
             used as follows:

                 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
                    $atime,$mtime,$ctime,$blksize,$blocks)
                        = stat($filename);

             Not all fields are supported on all filesystem
             types.  Here are the meaning of the fields:

               0 dev      device number of filesystem
               1 ino      inode number
               2 mode     file mode  (type and permissions)
               3 nlink    number of (hard) links to the file
               4 uid      numeric user ID of file's owner
               5 gid      numeric group ID of file's owner
               6 rdev     the device identifier (special files only)
               7 size     total size of file, in bytes
               8 atime    last access time since the epoch
               9 mtime    last modify time since the epoch
              10 ctime    inode change time (NOT creation time!) since the epoch
              11 blksize  preferred block size for file system I/O
              12 blocks   actual number of blocks allocated
So what you need is something like:
Code:
@files = qw(a b c d);

foreach(@files) {
  my($atime,$mtime,$ctime) = (stat($_))[7,8,9];
  print &quot;$_: $atime, $mtime, $ctime\n&quot;;
}
Cheers, Neil :)
 
Thanks,

So what I get out of this is
. : 1039405447 .. : 1039241429 Consistency.shtml : FaithTradition.shtml :
from the the code below. Much cleaner. Thanks

1)How do I get rid of the . / .. ?in the print statement?
2)How do I get the filename to coincide with the mtime?
ie:Consistency.shtml : 1039405447
FaithTradition.shtml : 1039241429

------------------------------------------------
Code:
foreach(@filenames) {
  my ($mtime) = (stat($_))[9];
  print &quot;$_ : $mtime\n&quot;;
}
In the begining
Let us first assume that there was nothing to begin with.
 
When I click on

Mark this post as a helpful/expert post!

I get a java script error

Does anybody know why? In the begining
Let us first assume that there was nothing to begin with.
 
It appears as if all your output is occuring on the same line. Are you using UNIX? If not, this could be related to the character(s) used to identify the end of a line. The [tt]perlport[/tt] man page says:
Code:
ISSUES
     Newlines

     In most operating systems, lines in files are terminated by
     newlines.  Just what is used as a newline may vary from OS
     to OS.  Unix traditionally uses \012, one kind of Windows
     I/O uses \015\012, and Mac OS uses \015.

     Perl uses \n to represent the &quot;logical&quot; newline, where what
     is logical may depend on the platform in use.  In MacPerl,
     \n always means \015.  In DOSish perls, \n usually means
     \012, but when accessing a file in &quot;text&quot; mode, STDIO
     translates it to (or from) \015\012.
In addition, you can remove the '.' and '..' entries by ensuring that each entry in your list is really a filename using something like:
[tt]
foreach(@filenames) {
next unless -f $_;
my ($mtime) = (stat($_))[9];
print &quot;$_ : $mtime\n&quot;;
}
[/tt]
Hope this helps :)
 
Actually, it looks like you are outputting as HTML?
HTML compacts white space (including line separators) by default. To get the effect you want, try something like:
Code:
print &quot;<p>$_: $mtime</p>\n&quot;;
[code]
Here is some more sample code:
[code]
opendir(DIR,&quot;.&quot;);
my @files = readdir(DIR);
close DIR;
@files = grep { -f } @files;
@times = map { { filename => $_, mtime => (stat($_))[9] } } @files;
@sorted = sort { $a->{mtime} <=> $b->{mtime} } @times;
@files = map { $_->{filename} } @sorted;
print &quot;<p>oldest file: $files[0]</p>\n&quot;;
print &quot;<p>newest file: $files[-1]</p>\n&quot;;
Hope this helps, cheers Neil :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top