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

Hi, How to get the most recently added file in a directory from perl script. The files contains numeric which cannot be sorted and get the recent file created.

Replies are listed 'Best First'.
Re: Recent file
by moritz (Cardinal) on Mar 05, 2009 at 08:41 UTC

      I think -M wouldn't help here, because it calculates based on the file modification, not creation time, as the OP wanted. AFIK there is no general way to obtain the creation time.

      -- 
      Ronald Fischer <ynnor@mm.st>

        Please note that you are making a common mistake with ctime - it is not create time, but inode change time.

        Update: Either I didn't read carefully or the parent updated their node. Probably the former.

        --MidLifeXis

Re: Recent file
by sathiya.sw (Monk) on Mar 05, 2009 at 13:24 UTC
    what did you mean by recent file ?
    Some body can say files which are created b/w 1s and 10 sec is recent file.
    Some body can say files which are created after the file named foo is recent file.
    So according to it you can get the solution.
    You can use the file::find.
    Use the find2perl for generating code as
    Find files which are created after FOO.
    find2perl find -newer foo
    #! /usr/bin/perl -w eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0; #$running_under_some_shell use strict; use File::Find (); # Set the variable $File::Find::dont_use_nlink if you're using AFS, # since AFS cheats. # for the convenience of &wanted calls, including -eval statements: use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune; sub wanted; my $AGE_OFfoo = -M 'foo'; # Traverse desired filesystems File::Find::find({wanted => \&wanted}, 'find'); exit; sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && (-M _ < $AGE_OFfoo) && print("$name\n"); }
    Sathiyamoorthy
Re: Recent file
by MidLifeXis (Monsignor) on Mar 05, 2009 at 15:38 UTC

    Store what you have already seen in a YAML, DBM::Deep, XML, or other file structure, and return those not in the file.

    You could also do this in a shell script using diff, grep, cut, and find if subdirectories are involved.

    --MidLifeXis

Re: Recent file
by ig (Vicar) on Mar 06, 2009 at 00:54 UTC

    Traditional *nix file systems and interfaces do not keep or make available file creation time but Windows file systems (FAT and NTFS) and interfaces do. From perl you can use Win32::FileTime to get the creation time of a file on a Windows platform.

    So, on most *nix platforms you cannot find the most recently created file but you may find that finding the most recently modified file is an adequate approximation to the most recently created file.

    On Windows you can check the creation time of each file to find the one with the most recent creation time.

Re: Recent file
by Anonymous Monk on Mar 05, 2009 at 08:40 UTC
    Search.
Re: Recent file
by puudeli (Pilgrim) on Mar 05, 2009 at 08:43 UTC

    See -X.

    --
    seek $her, $from, $everywhere if exists $true{love};