in reply to Most recently created file in a folder

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

Replies are listed 'Best First'.
Re^2: Most recently created file in a folder
by Anonymous Monk on Sep 07, 2004 at 13:50 UTC
    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