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

Can some offer a code snippet that prints the age of a file (in this case a win32 file) in seconds ?
update - I tried this since -> perl -le "print(time - ( stat(q[file.txt]) )[9])"
worked well
#!/usr/bin/perl -w # my $FileName = shift; print(time - ( stat(q[$FileName]) )[9]);
doesn't yield the same result, I know I am lame.

Replies are listed 'Best First'.
Re: File Age in Seconds ? (reask)
by merlyn (Sage) on Dec 22, 2005 at 21:48 UTC

      Heh. I once wrote some rather strange code that did something (I forget what) every 864 seconds. Years later, when challenged about that particular value, I looked at the questioner pityingly and said, in my most condescending tone, "1% of a day, of course."

      I then beat a hasty retreat. :)

Re: File Age in Seconds ? (need more handholding)
by ikegami (Patriarch) on Dec 22, 2005 at 22:57 UTC

    Re: Updated question

    Replace
    print(time - ( stat(q[$FileName]) )[9]);
    with
    print(time - ( stat($FileName) )[9]);

    q[...] is the same thing as '...'.

Re: File Age in Seconds ?
by borisz (Canon) on Dec 22, 2005 at 20:39 UTC
    $ctime = (stat( $filename ) )[10]; print $ctime;
    Boris
      I mean last modified, not creation time.
      Thanks
        perl -le "print(time - ( stat(q[file.txt]) )[9])"
        or perldoc -f stat

        Ted
        --
        "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
          --Ralph Waldo Emerson
Re: File Age in Seconds ? (reask)
by ptum (Priest) on Dec 22, 2005 at 21:47 UTC

    I prefer the by-name interface that File::stat permits:

    use strict; use File::stat; my $mtime = stat('/your/filename')->mtime;

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: File Age in Seconds ?
by Anonymous Monk on Dec 22, 2005 at 20:39 UTC
    Simple answer: use stat($filename):
    C:\Perl\bin>perl.exe -le "print(time - ( stat(q[perl.exe]) )[10])"

      Too bad: -M (see perldoc -f -X) does just that except that it's in days, not seconds. So one has to backconvert. Half-heartedly I think that maybe it may have been more convenient to stick with seconds, but I'm not really sure...

      Oh, and of course -M compares with script startup time, not time(), which may be a completely negligible difference or a very relevant one depending on the actual application!

      Well, it just to give the OP another option...