Use the -M test operator. It returns the age of a file in days, so, for example, if you want to process only files from yesterday, the code could look like this: (untested)

foreach my $file (@filelist) { if ( int( -M $file ) < 1 ) { # do something with a file } }

int () is used because -M returns values like 0.50 when a file is less than a day old.

If you want to perform a specific task, please tell what you want to do with the files, I'll be glad to write some example code.

Update:

As Utilitarian was kind enough to suggest, readdir works just fine. However, I would try using File::Find, its both simple and powerful, in case you'll need some extra functionality later on. Here's an example of it's use: (this time it's tested ;] )

#!/usr/bin/perl -w use strict; use File::Find; my $directory = "d:/usr"; print "List of files newer than yesterday in $directory:\n"; find(\&process, $directory); sub process { if ((int(-M) < 1) && (-f)) { print " $File::Find::name\n"; } }

Output:

List of files newer than yesterday in d:/usr: d:/usr/newfile1.txt d:/usr/newfile2.txt

The -f file test operator ensures we process only files, normally the output would contain d:/usr too. The process sub is executed for every file found in a directory, so feel free to modify it for your own needs.

Rgards,
Luke


In reply to Re: How to read list of files based on date ? by 1Nf3
in thread How to read list of files based on date ? by bh_perl

Title:
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, details, 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, summary, 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.