blackadder has asked for the wisdom of the Perl Monks concerning the following question:

Hi Gods

How can I find out which is the most recent file in a folder (recurssion is not required) by using Perl?

And if I use this;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, +$blksize,$blocks) = stat($filename); print "$atime : $mtime : $ctime\n";
I get the following results
1094560374 : 1094551336 : 1094551336
how can the above get converted to a more human like date? like this => 07 September 2004, 13:32:54

Thanks
Blackadder

Replies are listed 'Best First'.
Re: Most recently created file in a folder
by Zaxo (Archbishop) on Sep 07, 2004 at 13:06 UTC

    localtime and perhaps POSIX::strftime. To get the most recent, List::Util::max.

    $ perl -e'print scalar( localtime 1094551336), $/' Tue Sep 7 06:02:16 2004 $ perl -MPOSIX=strftime -e'print strftime "%d %B %Y, %T$/", localtime +1094551336' 07 September 2004, 06:02:16 $

    Added: For a shortcut to the most recent time (without any information on which file it was), stat the mtime of the directory. That will tell the last time its inodes were changed.

    After Compline,
    Zaxo

      Added: For a shortcut to the most recent time (without any information on which file it was), stat the mtime of the directory. That will tell the last time its inodes were changed.

      Unfortunately, the mtime for the directory will also be modified under other conditions as well (e.g. removal of a file), so it won't necessarily be correct.

Re: Most recently created file in a folder
by tachyon (Chancellor) on Sep 07, 2004 at 13:20 UTC

    You can use localtime or gmtime in scalar context to get a string like you desire from the Unix epoch times you have. In list context these function return a list of ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) so you can use sprintf to assemble almost format you want. POSIX strftime is yet another option. This snippet will output a sorted list.

    use constant ATIME => 9; use constant MTIME => 10; use constant CTIME => 11; my $dir = '.'; my $time = MTIME; my @files = map{ scalar localtime($_->[$time]). "\t" . $_->[0]} sort{ $b->[$time] <=> $a->[$time] } map{ [ $_, stat($_) ] } grep { -f } glob ( "$dir/*" ); print "$_\n" for @files;

    cheers

    tachyon

      Thanks to the enlightened Ones,...

      But I can't use this bit of code on a remote Win32 PC! I don't get any output!

      this all what I did
      use constant ATIME => 9; use constant MTIME => 10; use constant CTIME => 11; my $dir = "\\\\Sn007a_srv\\c\$\\program files\\common files\\sysadmin\ +\log\\ApplicationSynchronizer"; my $time = CTIME; my @files = map{ scalar localtime($_->[$time]). "\t" . $_->[0]} sort{ $b->[$time] <=> $a->[$time] } map{ [ $_, stat($_) ] } grep { -f } glob ( "$dir\\*" ); print "$files[0]\n";
      Any way I can use a remote folder please?
      Cheers
        Are you getting an error from the 'glob' call? To be honest, I'm not sure how to do that -- the documentation doesn't seem to mention it.

        My personal preference for reading a directory without recursion is to use opendir/readdir. That way, you can check for errors easier (e.g. does the full path exist, do you have permissions to access it, etc). You also don't have the overhead of using glob, nor relying on how the system decides to expand '*', which should be everything anyway (well, except for dot files under Unix).

        bluto

Re: Most recently created file in a folder
by Anonymous Monk on Sep 07, 2004 at 13:11 UTC
    On Unix you can't. Unix doesn't keep any record of "creation" of a file. It'll keep track of last modification time, and last inode modification time, and, unless your filesystem is mounted to not keep track of it, last access time. But not creation time.
Re: Most recently created file in a folder
by Grygonos (Chaplain) on Sep 07, 2004 at 13:15 UTC

    The doc on stat says that those times are the number of seconds since epoch..(also defined in the doc on stat). I would reccomend using Date::Calc if you really want a good human readable date. However, you can simply use the highest number returned in  $mtime if you're looking for the most recently modified file. I don't see stat giving you the create time however (I've never used stat, but just from a brief perusal of the docco on it). Good luck!