Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

stat is your friend

From the Perldocs..

Returns a 13-element list giving the status info for a file, either th +e file opened via FILEHANDLE, or named by EXPR. If EXPR is omitted, i +t 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 mea +ning 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 in seconds since the epoch 9 mtime last modify time in seconds since the epoch 10 ctime inode change time (NOT creation time!) in seconds since t +he epoch 11 blksize preferred block size for file system I/O 12 blocks actual number of blocks allocated
Here is a quick and dirty hack of how to do it without using dir..

#! /usr/bin/perl use strict; use warnings; use File::stat; my $dir = $ARGV[0] || "./"; my ($time1, $time2, $returnname); opendir (DIR, $dir) or die "Cannot open $dir: $!\n"; while( my $file = readdir( DIR ) ) { next if $file =~ /^\./; my $sb = stat($file); $time2 = $sb->mtime; if ($time2 > $time1) { $time1 = $time2; $returnname = $file; } #printf "File is %s, size is %s, mtime %s\n", $file, $sb->size, $sb +->mtime; } print "Newest file is $returnname\n";
Note the mtime is returned as an epoc time so if you want yesterdays file all you have to do is subtract 86400 seconds... type stuff...

Of all the things I've lost in my life, its my mind I miss the most.

In reply to Re: Return newest file by AcidHawk
in thread Return newest file by Smaug

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (6)
    As of 2021-04-21 05:58 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found