File::Find::Rule for files younger than 24 hours

A

Arvin Portlock

I'm trying to use File::Find::Rule to get files modified in
the last 24 hours. With the help of find2perl I got a plain
File::Find to work just fine, but File::Find is pretty horrible
so I'd prefer File::Find::Rule. This is on a Windows 2000
system:

My working File::Find version:

use File::Find;
my $rootdir = 'D:/MYDOCU~1/schema_validate';
File::Find::find({wanted => \&since_yesterday}, $rootdir);

sub since_yesterday {
my ($dev,$ino,$mode,$nlink,$uid,$gid);

(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
(int(-M _) < 1)
&& print("$File::Find::name\n");
}

My not-working File::Find::Rule version:

use File::Find::Rule;

my $rootdir = 'D:/MYDOCU~1/schema_validate';

my @files = File::Find::Rule->file()
->name( '*.pl' )
->mtime ("-1")
->in($rootdir);
foreach my $file (@files) {
print "$file\n";
}

I've actually tried a variety of things for the mtime() method
but either get nothing or I get everything. It's not clear to
me how to apply mtime() here.

Arvin
 
P

Paul Lalli

Arvin said:
I'm trying to use File::Find::Rule to get files modified in
the last 24 hours. With the help of find2perl I got a plain
File::Find to work just fine, but File::Find is pretty horrible
so I'd prefer File::Find::Rule. This is on a Windows 2000
system:

My working File::Find version:

use File::Find;
my $rootdir = 'D:/MYDOCU~1/schema_validate';
File::Find::find({wanted => \&since_yesterday}, $rootdir);

sub since_yesterday {
my ($dev,$ino,$mode,$nlink,$uid,$gid);

(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
(int(-M _) < 1)
&& print("$File::Find::name\n");
}

fwiw, you don't need all that excess that find2perl throws in. Your
whole sub body can be replaced with:

print "$File::Find::name\n" if int(-M) < 1;
My not-working File::Find::Rule version:

use File::Find::Rule;

my $rootdir = 'D:/MYDOCU~1/schema_validate';

my @files = File::Find::Rule->file()
->name( '*.pl' )
->mtime ("-1")
->in($rootdir);
foreach my $file (@files) {
print "$file\n";
}

I've actually tried a variety of things for the mtime() method
but either get nothing or I get everything. It's not clear to
me how to apply mtime() here.

Looking at
http://search.cpan.org/~rclamp/File-Find-Rule-0.28/lib/File/Find/Rule.pm#Matching_Rules,


it says to see C<stat> in perlfunc. Looking at that documentation, we
have:
9 mtime last modify time in seconds since the epoch

I would say you're getting confused between the return value of -M and
the mtime return value of stat().

it looks to me like the proper argument would be:
->mtime('> ' . (time() - 24*60*60))

That is, all files whose last modified times are greater than the
current time minus one day (give or take for DST).

Paul Lalli
 
D

Dr.Ruud

Arvin Portlock schreef:
my @files = File::Find::Rule->file()
->name( '*.pl' )
->mtime ("-1")
->in($rootdir);

http://www.perladvent.org/2002/11th/
does it like this:

my $last_week = time()-7*24*60*60;
my @files = File::Find::Rule->file
->name('*.mp3')
->size('>=200K')
->mtime("<$last_week")
->in('/home/mark/mp3');

but from `perldoc -f -M` I guess that should be ->mtime('>7').

I would expect '<1' to mean '<24h', but maybe the unit is exclusively
"seconds since epoch", like with stat.

See also File::Finder::Steps.
 
J

J. Gleixner

Arvin said:
use File::Find::Rule;

my $rootdir = 'D:/MYDOCU~1/schema_validate';

my @files = File::Find::Rule->file()
->name( '*.pl' )
->mtime ("-1")
->in($rootdir);
foreach my $file (@files) {
print "$file\n";
}

I've actually tried a variety of things for the mtime() method
but either get nothing or I get everything. It's not clear to
me how to apply mtime() here.

The value for mtime is the time the file was modified, in seconds. So
figure out the current number of seconds, minus 1 day, of seconds.

my $today = time();
my $days_ago = $today - 86400; #seconds in a day

my @files = File::Find::Rule->file()
->name( '*.pl' )
->mtime (">$days_ago")
->in($rootdir);


The documentation could probably add a few more examples.
 
P

Paul Lalli

Dr.Ruud said:
Arvin Portlock schreef:


http://www.perladvent.org/2002/11th/
does it like this:

my $last_week = time()-7*24*60*60;
my @files = File::Find::Rule->file
->name('*.mp3')
->size('>=200K')
->mtime("<$last_week")
->in('/home/mark/mp3');

but from `perldoc -f -M` I guess that should be ->mtime('>7').

You, like the OP, are confusing -M with stat(). The functions are
intimitely related (in so far that -M actually calls stat()) but their
return values are not the same.

-M returns the number of days since the file was last modified.
stat()'s mtime parameter is the number of seconds since the epoch that
the file was last modified.

Paul Lalli
 
A

Arvin Portlock

fwiw, you don't need all that excess that find2perl throws in. Your
whole sub body can be replaced with:

print "$File::Find::name\n" if int(-M) < 1;

That's nice. My biggest argument with File::Find is that
it's hard to encapsulate it within a larger program. I
see a couple of different ways to do it but they're both
ugly and not very perl-ish.
it looks to me like the proper argument would be:
->mtime('> ' . (time() - 24*60*60))

And that did the trick, Thank you. Believe it or not I
tried something almost identical to that. I think I
didn't have the > in there though. Probably I would have
stumbled on it sooner or later.

Thanks Everybody!
 
R

Randal L. Schwartz

Arvin> my @files = File::Find::Rule->file()
Arvin> -> name( '*.pl' )
Arvin> -> mtime ("-1")
Arvin> -> in($rootdir);
Arvin> foreach my $file (@files) {
Arvin> print "$file\n";
Arvin> }

In File::Finder, that'd be:

my @files = File::Finder
->name('*.pl')
->mtime('-1')
->in($rootdir);

File::Finder uses find(1) primitives, which I find easier to understand
than File::Find::Rule's mix of find(1) and ad-hoc constructs.
 
D

Dr.Ruud

Paul Lalli schreef:
You, like the OP, are confusing -M with stat().

Well, I was leaving it to the OP to find out which formats are allowed,
qr{[+-><]?\d+} days or qr{\d+} epoch-seconds or even both and more. That
information is quite hard to find, and obscured by examples everywhere
that freely use all kinds of formats with mtime.
The documentation of File::Find::Rule limits it to epoch seconds and
Number::Compare semantics. It has ->modified for -M, and ->mtime for
stat[9].

The functions are
intimitely related (in so far that -M actually calls stat()) but their
return values are not the same.

-M returns the number of days since the file was last modified.
stat()'s mtime parameter is the number of seconds since the epoch that
the file was last modified.

Yes, that was not the problem, but nowhere (that I could find) was made
clear whether File:Find's mtime is in (relative) days, or in epoch
seconds, or that even other formats like '2005-12-25' are OK too.


From the documentation of 'nephew' File::Finder::Steps:

my $big_or_old = File::Finder
->type('f')
->left
->size("+100")->or->mtime("+90")
->right;
find($big_or_old->ls, "/tmp");
 
A

Arvin Portlock

Arvin said:
I'm trying to use File::Find::Rule to get files modified in
the last 24 hours.
My not-working File::Find::Rule version:

use File::Find::Rule;

my $rootdir = 'D:/MYDOCU~1/schema_validate';

my @files = File::Find::Rule->file()
->name( '*.pl' )
->mtime ("-1")
->in($rootdir);
foreach my $file (@files) {
print "$file\n";
}

This thread has become very interesting for me. Thanks for
all the input. One interesting thing that was causing me
problems and contributed to my overall confusion vis-à-vis
mtime () was that my program wouldn't work if I included
the program's directory in the search path. Something odd
happens when the program attempts to retrieve itself in
the list of files. No error message, I would simply get no
results. I didn't notice this problem until a few minutes
ago, so was trying a wide variety of crazy things to get
it to work. A few evals done early might have revealed
the problem.

Might be a Windows-specific problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top