Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Checking file Mod date

by LostS (Friar)
on Jul 19, 2001 at 17:21 UTC ( #98062=perlquestion: print w/replies, xml ) Need Help??

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

Hey, OK... lets see I am making this script that is supposed to check a set of files in a directory and check the mod date. If the file has been edited in the last 7 days... Then e-mail it to these people. Now I have the Get the files doing this:
opendir (DIR, '/www/htdocs/data/banner_rev/test'); @list = @pics = grep /\.(txt)/, readdir(DIR); closedir (DIR);
However I am unclear on how to check the file's mod date... Any suggestions on where to look for this?? Thanks, Billy S.

Replies are listed 'Best First'.
Re: Checking file Mod date
by HyperZonk (Friar) on Jul 19, 2001 at 17:26 UTC
    See the -X functions, in particular -M.
Re: Checking file Mod date
by sifukurt (Hermit) on Jul 19, 2001 at 18:27 UTC
    Not to complicate matters, but you could also use stat(). Using stat() is particularly useful if you want to see the actual datestamp that the file was last modified, whereas -M only returns the number of days since modification. It is possible to get get the actual datestamp if you've only used -M, but you'd have to use Date::Manip. To do the whole process using stat(), you could do it something like this:
    $sevenDays = 60 * 60 * 24 * 7; $now = time(); foreach $file ( @files ) { $mod_time = ( stat($file) )[9]; if ( $now - $sevenDays < $mod_time ) { # Our file has been modified w/i last 7 days } else { # Hasn't been modified w/i last 7 days } }

    It is then a trivial matter to retrieve the exact date of the modification, like so:
    $date = localtime( (stat($file) )[9] );
    Or, using the first example, since we've already got the modification time stored in $mod_time, we'd simply have to use $date = localtime( $mod_time ).
Re: Checking file Mod date
by Hofmator (Curate) on Jul 19, 2001 at 17:29 UTC
    perldoc -f "-X"

    -- Hofmator

      Does anyone have an example on how to use this function...
      Like what is the output? How will it display the last mod date??

        The -M filetest operator gives the number of days since modification since startup of the script, so:

        my $age = -M $file; if ($age < 7) { # do something } # or directly if (-M $file < 7) { # do something }

        -- Hofmator

Re: Checking file Mod date
by kebex (Initiate) on Jul 19, 2001 at 18:33 UTC
    You probably want the stat() command.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2023-12-09 06:13 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (37 votes). Check out past polls.