Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

find the latest created file in a directory

by doubledecker (Scribe)
on Apr 24, 2013 at 13:40 UTC ( #1030401=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks

I'm trying to find the latest file created or modified by time in a directory and using the following code

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; my @newest = @{$sorted_files[0]}; my $name = pop(@newest);

But the problem is that in my perl script, I am using File::stat module and inturn 'stat' used inside 'map' is referring to File::stat module but not core function. This results in getting a wrong file name. How can I resolve this issue. any help is appreciated.

Replies are listed 'Best First'.
Re: find the latest created file in a directory
by Tux (Canon) on Apr 24, 2013 at 13:57 UTC

    use the CORE:: prefix:

    opendir my $dh, $DIR or die "Error opening $DIR: $!"; my @files = sort { $b->[10] <=> $a->[10] } map {[ $_, CORE::stat "$DIR/$_" ]} grep !m/^\.\.?$/, readdir $dh; closedir $dh; my ($name, @stat) = @{$files[0]};

    Enjoy, Have FUN! H.Merijn
      I guess you haven't actually tried your code... :)

      What about
      sort { $b->[1]->[10] <=> $a->[1]->[10] }
      instead of your sort ...?

        Actually, I did:

        $ cat test.pl #!/pro/bin/perl use 5.016; use warnings; my $DIR = "usr"; opendir my $dh, $DIR or die "Error opening $DIR: $!"; my @files = sort { $b->[10] <=> $a->[10] } map {[ $_, CORE::stat "$DIR/$_" ]} grep !m/^\.\.?$/, readdir $dh; closedir $dh; use DP;DDumper $files[0]; $ ls -lrt usr total 16 -rw-rw-rw- 1 merijn users 2588 Jun 18 2004 Honda.gif -rw-rw-rw- 1 merijn users 2388 Feb 20 2007 Honda.png -rw-rw-rw- 1 merijn users 3774 Feb 20 2007 Honda.ico drwxrwxrwx 2 merijn users 4096 Oct 22 2012 terminfo $ perl test.pl [ 'terminfo', 2065, 15481507, 16895, 2, 203, 100, 0, 4096, '1366762203', '1350940900', '1350940900', 4096, 8 ] $ touch usr/new $ perl test.pl [ 'new', 2065, 11278617, 33206, 1, 203, 100, 0, 0, '1366816276', '1366816276', '1366816276', 4096, 0 ] $

        Enjoy, Have FUN! H.Merijn
Re: find the latest created file in a directory
by johngg (Canon) on Apr 24, 2013 at 20:45 UTC

    Using &List::Util::reduce, can save having to sort.

    $ perl -MList::Util=reduce -Mstrict -Mwarnings -E ' my $newest = reduce { $a->[ 1 ] > $b->[ 1 ] ? $a : $b } do { opendir my $dotDH, q{.} or die qq{opendir: .: $!\n}; map { [ $_, ( stat $_ )[ 9 ] ] } grep { -f and not -l } readdir $dotDH; }; say qq{Newest file - $newest->[ 0 ]};' Newest file - spw1029421 $

    And that really is the newest file in the directory :-)

    Cheers,

    JohnGG

Re: find the latest created file in a directory
by Anonymous Monk on Apr 24, 2013 at 13:44 UTC

    if you can't figure out the code, use File::Find::Age

    # source: stackoverflow. thanks very much

    stackoverflow is the name of a website, not a source (not a url)

Re: find the latest created file in a directory
by pvaldes (Chaplain) on Apr 24, 2013 at 14:18 UTC
    or use find2perl
Re: find the latest created file in a directory
by Krambambuli (Curate) on Apr 24, 2013 at 14:17 UTC
    Maybe this comes close to what you want:
    #!/usr/bin/perl use strict; use warnings; use File::stat; my $DIR = './'; opendir(my $DH, $DIR) or die "Error opening $DIR: $!"; my @files = map { [ stat "$DIR/$_", $_ ] } grep( ! /^\.\.?$/, readdir( $DH ) ); closedir($DH); sub rev_by_date { $b->[0]->ctime <=> $a->[0]->ctime } my @sorted_files = sort rev_by_date @files; my @newest = @{$sorted_files[0]}; my $name = pop(@newest); print "Name: $name\n";

    Krambambuli
Re: find the latest created file in a directory
by sureshepuri (Initiate) on Apr 25, 2013 at 06:58 UTC
    This can be done through unix command aswell. Not sure whether this suits your requirements #!/usr/bin/perl use strict; use warnings; my $latest_file=`find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "`;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1030401]
Approved by blue_cowdawg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2022-12-02 23:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?