Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by mikesolomon (Novice)
on Feb 22, 2012 at 12:02 UTC ( [id://955504]=perlquestion: print w/replies, xml ) Need Help??

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

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

Any help acheiving this would be much appreciated

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>}; }

Replies are listed 'Best First'.
Re: use file::find to find files modified in last 5 days
by nemesdani (Friar) on Feb 22, 2012 at 12:24 UTC
    Check if a file is older than certain days:
    if (-M FILEHANDLE > $howmanydays)
    No reason to overcomplicate it.
      Thanks for your comment, you save my time.
Re: use file::find to find files modified in last 5 days
by thargas (Deacon) on Feb 22, 2012 at 13:10 UTC

    If you're going to use perl, you really need to learn about perldoc. The most important command you need to start with is perldoc perltoc which asks perldoc to show you the perl TOC (Table Of Contents) which lists all the other things you can ask perldoc about. You can also go to perlfunc if you'd rather have the info on a web-page.

    Do perldoc perlfunc and look for "file test". This will show you all the various file testing operators, like the -T you're already using and the -M you want here.

      Thanks

      don't know why but didn't think of a simple -M test

      now working

      return unless -M $file < $days;
      If you're going to use perl, you really need to learn about perldoc
      How could you tell he was on a Microsoft system? Because he said file::find in his title?
        whiskey - tango - foxtrot.

        How did "Microsoft" pop into your mind, based on thargas's words?
Re: use file::find to find files modified in last 5 days
by wwe (Friar) on Feb 22, 2012 at 12:42 UTC
    The docs of File::Find show this example:
    find2perl / -name .nfs\* -mtime +7 \ -exec rm -f {} \; -o -fstype nfs -prune
    produces something like:
    sub wanted { /^\.nfs.*\z/s && (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) && int(-M _) > 7 && unlink($_) || ($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_ +))) && $dev < 0 && ($File::Find::prune = 1); }
    what's wrong with it?
      what's wrong with it?
      What's wrong? How about: completely incomprehensible to anybody but Unix gurus.

        True, it's complicated. But a few minutes spent looking at the man page for find2perl and the POD for File::Find should permit a user to piece things together.

        It's not always necessary to understand *everything* about a subject in order to make use of the tools. I don't know how to tear apart and rebuild my car's engine, but that doesn't prevent me from driving it.

        Alex / talexb / Toronto

        "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      I had looked at that but couldn't work out how to integrate it into my script

        As brother thargas has explained, you need to look at the documentation. Type perldoc File::Find and you'll be presented with an example in the Synopsis section that should answer your question:

        use File::Find; find(\&wanted, @directories_to_search); sub wanted { ... }
        You pass File::Find::find two parameters: a reference to a function that will be called when a matching file is found, and a list of directories to be searched. And it should Just Work(tm).

        Alex / talexb / Toronto

        "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: use file::find to find files modified in last 5 days
by CountZero (Bishop) on May 27, 2013 at 11:46 UTC
    File::Find::Rule makes this very easy:
    use Modern::Perl; use File::Find::Rule; say join "\n", recent_files( 'c:/data/strawberry-perl/perl/script-chrome', 5 ); sub recent_files { # path-to-search, maximum age in days my $time = time - 86400 * $_[1]; return File::Find::Rule->file()->mtime(">$time")->in( $_[0] ); }

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://955504]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 02:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found