Re: Reading a directory and getting file dates
by sasikumar (Monk) on Jun 01, 2005 at 20:09 UTC
|
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
| [reply] [d/l] |
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";
}
| [reply] [d/l] |
Re: Reading a directory and getting file dates
by cool_jr256 (Acolyte) on Jun 01, 2005 at 20:19 UTC
|
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";
}
}
}
| [reply] [d/l] |
|
|
my $mtime = (stat("/home/user/ptest/$dir"))[9];
...at least it's easier to read ;)
--
b10m
All code is usually tested, but rarely trusted.
| [reply] [d/l] |
|
|
Very true :)...I was a little too eager on the reply :), but at least it shows the available info one can get from 'stat'...
| [reply] |
|
|
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. | [reply] [d/l] |
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?
| [reply] |
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.
| [reply] |
|
|
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).
| [reply] |
|
|
| [reply] |
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 '*'"
| [reply] [d/l] |
Re: Reading a directory and getting file dates
by injunjoel (Priest) on Jun 01, 2005 at 20:09 UTC
|
| [reply] |