Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Last Access Time of files.

Status
Not open for further replies.

awolff01

Programmer
Jun 17, 2003
55
US
Hello,

I am looking for a snippet of code that shows how to get
all files that have been accessed in the last 300 seconds into an array?

Or do I have to bring all the files into an array first then go thru each one determining which ones were accessed in the last 5 minutes?

Much appreciated,

Alex
 
I don't think there's a way to get *only* the names of files that have been accessed within a time frame. You need the name in order to call stat() to find the access time, so you have to get all the names first.

$now = time();
opendir(D, dirname);
@accessedfiles = ();
foreach $f (readdir(D)) {A
if ( $f eq "." or $f eq ".." ) {
next;
}
$st = stat($f);
if ( ($now - $st->atime) < 300 ) {
push @accessedfiles, $f;
}
}
closedir(D);

 
Thanks. Any idea why I would get this error message?
Can't call method &quot;atime&quot; on an undefined value at testpl.pl line 10.

<----Script--->
#!/opt/perl/bin/perl
use File::stat;
$tsec = time();
opendir(D, '/sched/slbsprod');
foreach $f (readdir(D)) {
if ( $f eq &quot;.&quot; or $f eq &quot;..&quot;) {
next;
}
$st = stat($f);
if (($tsec - $st->atime) < 300) {
print &quot;$f\n&quot;;
}
}




 
My initial thought would have been something like this:
Code:
#!/opt/perl/bin/perl

for(glob '/sched/slbsprod/*')
{
    print &quot;$_\n&quot; if((-A * 24*60) < 5);
}
-A returns accessed time in days (I think minus the script's runtime, so hope that's not a problem) so cast that over into seconds for the compare (or leave it in minutes...)

But to answer your last question,
Code:
use File::Stat qw/:stat/;
to import the module's stat function and override the built-in.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top