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

Trying to get all files in my directory and the last access time AND the last modification time. I keep getting Dec 31, 1969 for my times. What did I do wrong???
$newfile = "/perl/bin/newr"; opendir(DIR,$newfile); @files = readdir(DIR); close(DIR); for(@files) { print "$_\n"; $_ = $lastaccess = localtime ( ( stat($_)) [8] ); #print "$_\n"; $_ = localtime ( (stat($_))[9] ); print "$_\n"; } print "./"; print "\n";

Replies are listed 'Best First'.
Re: stat times
by Ferret (Scribe) on Aug 12, 2002 at 18:07 UTC

    Your problem's pretty clear, actually - you're overwriting $_ in your loop there, and trying to stat the results anyway.

    My recommendation is to stat once and store it in an array, like below, not only so you don't have that problem, but because it's more efficient - only one stat call.

    Of course, there are better ways to do what you're doing, like Shendal's suggestion, but I figured you'd like to know what you did wrong.

    for(@files) { my @stat = stat($_); print "$_\n"; $_ = $lastaccess = localtime ( $stat[8] ); #print "$_\n"; $_ = localtime ( $stat[9] ); print "$_\n"; }

      While the code is ugly, it is unfortunately legal to use and abuse our default variable like that.

      Invulnerable. Unlimited XP. Unlimited Votes. I must be...
              GhodMode
Re: stat times
by Shendal (Hermit) on Aug 12, 2002 at 18:04 UTC
    How about something like this?
    #/usr/bin/perl -w use strict; # always a good idea my $newfile = '/perl/bin/newr'; # consider File::Find, if you'd like to be recursive # also, always check the return value of opendir # (I suspect this may be the root of your problems) opendir(DIR,$newfile) or die "Unable to opendir!"; my @files = readdir(DIR); close(DIR); foreach (@files) { print "File: $_\n"; my($atime,$mtime) = (stat($_))[8,9]; print "atime: " . localtime($atime) . "\n"; print "mtime: " . localtime($mtime) . "\n"; }


    Cheers,
    Shendal
Re: stat times
by DamnDirtyApe (Curate) on Aug 12, 2002 at 18:10 UTC

    You are overwriting $_ in your first operation, then trying to stat it in your second. It doesn't contain the file name anymore, so you don't get what you're looking for.


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: stat times
by GhodMode (Pilgrim) on Aug 12, 2002 at 18:41 UTC

         stat can't see the file it's trying to get the stats on. Put chdir($newfile) before the for loop like this

    $newfile = "/perl/bin/newr"; opendir(DIR,$newfile); @files = readdir(DIR); close(DIR); chdir ($newfile); for(@files) { print "$_\n"; $filename = $_; $_ = $lastaccess = localtime ( (stat($_))[8] ); $_ = localtime ( (stat($_))[9] ); print "accs time : $lastaccess\n"; print "mod time : $_\n"; }
         While that will fix it, your code hard to read and trying to figure out what $_ is at any given time hurts my head. Please do it like this...
    use strict; my $newfile = "/perl/bin/newr"; opendir(DIR,$newfile); my @files = readdir(DIR); close(DIR); chdir ($newfile) or die "Could not change to $newfile directory\n$!\n"; for(@files) { my $filename = $_; my $accesstime = localtime ( (stat($filename))[8] ); # Doing a file stat on the special _ filehandle tells # Perl to use whatever happened to be in memory from the # previous stat rather than going out to the operating # system again (Llama: 3rd Ed., p.166) my $modtime = localtime ( (stat(_))[9] ); print "file name : $filename\n"; print "accs time : $accesstime\n"; print "mod time : $modtime\n"; }

    Invulnerable. Unlimited XP. Unlimited Votes. I must be...
            GhodMode
      Thank you for all replies and helping me!
Re: stat times
by snafu (Chaplain) on Aug 12, 2002 at 18:45 UTC
    I wrote a script that did this very thing. It's called macls. (follow the link). Go ahead and use snippets from my script if you want...or use the script for your needs. This little baby has actually come to my rescue many times.

    _ _ _ _ _ _ _ _ _ _
    - Jim
    Insert clever comment here...