use file::find to find files modified in last 5 days

S

STD

I have written a script to find files

I now want to be able to pass it a days parameter and only show files
modified in the last x days

How do I do this

My script so far is below

use strict;
use lib 'jumi';
use WebLib;
use File::Find;
use File::Basename;

my ( $folder, $tofind, $tofile, $exclude, $cs ) = @ARGV;
my @tofind = split '&&',$tofind;

#strip whitespace
@tofind = grep(s/\s*$//g, @tofind);
@tofind = grep(s/^\s*//g, @tofind);

foreach(@tofind) {print "|$_|<br>";}

print "<p>Excluded:<br>$exclude</p>";

find(\&wanted, $folder);

print qq {<script type="text/javascript">colourRows();</script>};

sub wanted {

my $file = $File::Find::name;

return unless -T $file;
return unless $file =~ /$tofile/;
return if $file =~ /$exclude/ && $exclude;

if ($tofind) {
open F, $file or print "couldn't open $file\n" && return;
my $file_contents = do { local $/; <F> };
close F;
my $print = 'Y';
foreach (@tofind) {
if ($cs == 1){
if ($file_contents !~ /$_/m) {$print = 'N'};
} else {
if ($file_contents !~ /$_/mi) {$print = 'N'};
}


}

if ($print eq 'Y'){
print qq {<tr><td><a href="index.php?
option=com_content&view=article&id=21&file=$file" target="_blank">
$file</a></td></tr>};
} #end if

} else {
print qq {<tr><td><a href="index.php?
option=com_content&view=article&id=21&file=$file" target="_blank">
$file</a></td></tr>};

}
 
J

Justin C

I have written a script to find files

I now want to be able to pass it a days parameter and only show files
modified in the last x days

Have a look at the 'stat' and 'localtime' functions, they should give
you a clue at to your next step.

Justin.
 
S

STD

The -M operator gives you the age of a file in days.

Ben

Thanks

I have now added

return unless -M $file < $days;

All working

Don't know why I didn't think of I before - doh
 
T

Tim McDaniel

I just don't know how people remember all of those.

There's a lot I don't remember about Perl. I try to remember that
there's a way to do such-and-so and a vague idea of where to look it
up. For "-M", I tried "man perlop", but it's not there -- it's in
"man perlfunc", which makes more sense come to think of it.
 
J

Jim Gibson

Tim McDaniel said:
There's a lot I don't remember about Perl. I try to remember that
there's a way to do such-and-so and a vague idea of where to look it
up. For "-M", I tried "man perlop", but it's not there -- it's in
"man perlfunc", which makes more sense come to think of it.

'perldoc -f -x' lists all of the file test operators. That is what I
can remember.
 
R

Randal L. Schwartz

STD> I have written a script to find files
STD> I now want to be able to pass it a days parameter and only show files
STD> modified in the last x days

STD> How do I do this

Check out my File::Finder in the CPAN which has an interface that mimics
find(1) very closely.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top