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

Currently I am trying to find the age of certain files in days. So I am trying to use -M to do that.

The prob is that -M doesnt seem to be working on these files. Is it the filename, or is it a permissions prob?

Here is the code that I am using:

while ($file = readdir (DIR)) { if ($file !~ /^\.+$/) { # GET THE AGE OF THE FILE IN DAYS # -M gives us the age of the file in days when script started. # It needs to be rounded! $age = -M ($file); $age = sprintf ("%0.0f", $age); print "FILE: $file:\n\tAGE: $age\n"; if ($age > 2) { print "DELETE ME!\n"; } else { print "SAVE ME\n"; } } }
Age is blank and after the sprintf statement it is 0, which is NOT correct!

Here is a sample listing of the files that I am trying to determine the age of (in days).

7 -rw-rw-r-- 1 root 6441 Jun 17 00:00 2001-06-16T194921Z_0 +1_N16487427_RTRIDST_0_HEALTH-DOCTORS-UNION.XML 6 -rw-rw-r-- 1 root 5148 Jun 17 04:55 2001-06-17T082435Z_ +01_L17466768_RTRIDST_0_HEALTH-ISRAEL-TEVA.XML 9 -rw-rw-r-- 1 root 8558 Jun 17 13:05 2001-06-17T163225Z_ +01_N17218051_RTRIDST_0_-FINANCE-IPO-OUTLOOK.XML 10 -rw-rw-r-- 1 root 9490 Jun 17 13:50 2001-06-17T171804Z_ +01_N17360454_RTRIDST_0_CONGRESS-HEALTH.XML 7 -rw-rw-r-- 1 root 6176 Jun 18 03:05 2001-06-18T063312Z_ +01_ZUR821703_RTRIDST_0_HEALTH-MEDICA-IMPLANTS-U RGENT.XML
Thanks Robert

Edit: chipmunk 2001-06-20

Replies are listed 'Best First'.
Re: How can I get -M to work properly?
by clintp (Curate) on Jun 20, 2001 at 19:35 UTC
    A few things. First, what was $age before you did the sprintf thingy? That'd be much more useful than seeing it afterwards.

    If it's undef, then make sure that $file contiains enough the pathname to the file. Items returned by opendir() don't have a path appended to them.

    Lastly (and tangental), has it been more than two days (rounded) since June 17 00:00?

      Man, Thanks for helping me here! PerlMonks Rules!

      I didnt give it the bloody path

      Thanks for saving a poor monk from programmer's frustration!

      Robert

Re: How can I get -M to work properly?
by azatoth (Curate) on Jun 20, 2001 at 19:33 UTC
Re: How can I get -M to work properly?
by converter (Priest) on Jun 20, 2001 at 20:09 UTC

    It's always a good idea to check the results of file tests.

    $ perl -e '$age = -M "foo.txt" or die "problem: $!"; print "Age: $age\n"'
    Age: 6.13123842592593
    
    $ perl -e '$age = -M "foobar.txt" or die "problem: $!"; print "Age: $age\n"'
    problem: No such file or directory at -e line 1.
    
Re: How can I get -M to work properly?
by buckaduck (Chaplain) on Jun 20, 2001 at 19:36 UTC
    If DIR is not the current directory, you will need to prepend the path to each filename before the -M test.
    $age = -M "$path/$file";

    buckaduck

Re: How can I get -M to work properly?
by bikeNomad (Priest) on Jun 20, 2001 at 19:36 UTC
    This is unlikely to be a permissions problem, as you can read the directory to get the names of the files (I'm assuming your script prints out the names).

    You might want to try listing the mod times in your script:

    my @stat = stat($file); print $file, " ", scalar(localtime($stat[9])), "\n";

    update: -M returns a float.