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

Hi all,

I'm sure I'm missing something obvious here, but I have checked Q&A and can't seem to find where I'm going wrong.

I am using stat to return the 'mtime' for a files in a given Win32 directory. I want to ensure that no file in the directory is older than 20min.

I'm getting some strange results...

use strict; my $mytime=(time); my ($File,$FileStat,$CallOutDir); opendir(CallOut, $CallOutDir) || die "$!\n"; foreach $File (readdir(CallOut)) { next if $File=~/^\./; if ($FileStat = (stat $File)[9]){ if ($FileStat <= ($mytime - 1200)) { print "File older than 20min found. The file name is $File"); #send mail here.... last; } } }

Please help o suppliers of wisdom.

Thanks, Smaug.

Replies are listed 'Best First'.
Re: File stat problem
by Abigail-II (Bishop) on Feb 26, 2004 at 13:48 UTC
    if ($FileStat = (stat $File)[9]){
    Are you sure your current working directory is $CallOutDir? Else, you might want to change that line to
    if ($FileStat = (stat "$CallOutDir/$File") [9]) {

    Abigail

      That's it!!! AAARRRRRGGGG!!!

      Thanks a million!
Re: File stat problem
by ambrus (Abbot) on Feb 26, 2004 at 15:02 UTC

    Instead of the stat and time functions, you can just use the -M function that tells you the age of a file in days, as a floating point number. That's a bit more convenient.

Re: File stat problem
by maa (Pilgrim) on Feb 26, 2004 at 13:41 UTC

    Hi... you're comparing $mtime with itself minus 1200... perhaps you want my $now=time(); somewhere :-)

    HTH - Mark

      Hi,
      I left out the following top section of the code....sorry!!!
      use strict; my $mytime=(time); my ($File,$FileStat,$CallOutDir);

      Cheers, Smaug.
Re: File stat problem
by derby (Abbot) on Feb 26, 2004 at 13:43 UTC
    What are the strange results? (Besides the unbalanced paren on the print line which I'm assuming was just a mistake).

    -derby
      Hi, Yip, the ')' is as a result of some fat fingers and non-existant typing skills.
      The results are mixed, at first I was getting only the first file older than 20min returned. After that the script stopped. I found that '$FileStat' was empty.
      I made some changes, and now even though there are files that are 3 weeks old, it does not find any.

      Thanks, Smaug.
Re: File stat problem
by TomDLux (Vicar) on Feb 26, 2004 at 17:42 UTC

    While it has nothing to do with the problem you are experiencing, there are some oddities in your code. I wonder if there are reasons why you are doing them.

    my $mytime = (time);

    time() returns a scalar, which you are assigning to a scalar. Why do you place it inside a list? Invoking array context makes no difference in the return value. Or were you intending to have the parentheses at the end, indicating a function call with no parameters, as in

    my $mytime = time();
    if ($FileStat = (stat $File)[9]){

    Why do you have the stat return value as the condition of an if? Or was that part of your debugging, trying to figure out the odd results you were getting. I would suggest verifying each file exists, if you have any doubt or want to be robust, and then confidently and boldly getting and using it's mtime.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      my $mytime = (time); does not imply array context for time, only my ($mytime)= time; or (my $mytime)= time would do that.