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

I want to read the files in a directory and get the files name and creation date. The problem is getting the date. I'm able to read the files without a problem, using something like
opendir(DIR, "t:\\") or die "can't opendir $dirname: $!"; @files = readdir(DIR); closedir(DIR); foreach $file (@files) { print "$file\n";
Ideally I'd like to have the file name in one variable $file and the date in another $date. If I don't find a handy way of doing this then I'm going to have to do something stupid like load the list of files into an array and then run through the array and get the properties for each file (stupid). Please help.

Replies are listed 'Best First'.
Re: Reading a directory and getting file dates
by sasikumar (Monk) on Jun 01, 2005 at 20:09 UTC
    Hi

    use File::stat; opendir(DIR, "t:\\") or die "can't opendir $dirname: $!"; @files = readdir(DIR); closedir(DIR); foreach $file (@files) { print "$file\n"; $statusFile=stat(<path\to\file\filename>); print $statusFile->mtime; ##Modification time }

    For more details look at File::stat in CPAN
    Thanks
    SasiKumar
Re: Reading a directory and getting file dates
by holli (Abbot) on Jun 01, 2005 at 20:26 UTC
    use strict; use POSIX qw (strftime); my $path = "c:\\"; opendir(DIR, $path) or die "can't opendir $path: $!"; my @files = readdir(DIR); closedir(DIR); foreach my $file (@files) { my ($mtime, $ctime) = (stat("$path$file"))[9,10]; print "$path$file was created at ", strftime ("%c", localtime($cti +me)), " and last modified at ", strftime ("%c", localtime($mtime)), " +\n"; }


    holli, /regexed monk/
Re: Reading a directory and getting file dates
by cool_jr256 (Acolyte) on Jun 01, 2005 at 20:19 UTC
    Try this:
    opendir(DIR,"/home/user/ptest"); while($dir=readdir(DIR)) { if(($dir ne ".")&&($dir ne "..")) { if(-d "/home/user/ptest/$dir") { ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $ati +me,$mtime,$ctime,$blksize,$blocks)=stat("/home/user/ptest/".$dir); print "FILE: ".$dir." Time: $mtime\n"; } } }

      If all you want is the $mtime, you might as well use this:

      my $mtime = (stat("/home/user/ptest/$dir"))[9];

      ...at least it's easier to read ;)

      --
      b10m

      All code is usually tested, but rarely trusted.
        Very true :)...I was a little too eager on the reply :), but at least it shows the available info one can get from 'stat'...
Re: Reading a directory and getting file dates
by TedPride (Priest) on Jun 01, 2005 at 21:02 UTC
    Depending on what you want to do with the creation date, you might also look into the -C flag:
    $date = -C $file;
    This returns the number of days since the file was created, which allows you to easily determine which files are over a certain age if, for instance, you're trying to clean up your directory.

    Otherwise, use stat.

Re: Reading a directory and getting file dates
by jpeg (Chaplain) on Jun 01, 2005 at 20:16 UTC
    Loading filenames into an array and looping through it, stat'ing each file is the canonical way to do this. I'm curious - why do you think that's stupid?

    --
    jpg
Re: Reading a directory and getting file dates
by tlm (Prior) on Jun 01, 2005 at 20:07 UTC

    I'm not sure whether this would work on Windows, but in Unix I would use stat.

    Update: bart is right, the ctime field of Unix's stat's output is a poor substitute for the time of a file's creation. I don't recall ever needing time of creation for files (I always use the time of last modification), so I overlooked the "creation" part of the OP's question. My apologies. Fortunately, the Windows version of stat (as described in perlport) appears to be more suitable to the OP's goal than the Unix one would have been. So my suggestion turns out to be fortuitously on target.

    the lowliest monk

      Would you? Have you also checked that there is actually no field for the creation date? The closest you get is ctime, "inode change time", which might be creation time under some circumstances.

      perlport says:

      The "inode change timestamp" (the -C filetest) may really be the "creation timestamp" (which it is not in UNIX).
Re: Reading a directory and getting file dates
by sh1tn (Priest) on Jun 01, 2005 at 20:36 UTC
    perl -e "map{ -f and print pack("A25",$_), (stat)[10], $/ }glob '*'"


Re: Reading a directory and getting file dates
by injunjoel (Priest) on Jun 01, 2005 at 20:09 UTC
    Greetings all
    It sounds like you will need to run some filetest operations for that. Here is the documentation.

    Update! Read too quickly... I agree with tlm use stat.

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo