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

Discussion in 'Perl Misc' started by STD, Feb 22, 2012.

  1. STD

    STD Guest

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

    }
     
    STD, Feb 22, 2012
    #1
    1. Advertising

  2. STD

    Justin C Guest

    On 2012-02-22, STD <> wrote:
    > 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.

    --
    Justin C, by the sea.
     
    Justin C, Feb 22, 2012
    #2
    1. Advertising

  3. STD

    STD Guest

    On Feb 22, 12:28 pm, Ben Morrow <> wrote:
    > Quoth 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

    >
    > 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
     
    STD, Feb 22, 2012
    #3
  4. STD

    STD Guest

    On Feb 22, 12:28 pm, Ben Morrow <> wrote:
    > Quoth 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

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


    Thanks

    Don't know why I didn't think of a simple -M test

    Now all working
     
    STD, Feb 22, 2012
    #4
  5. STD

    Justin C Guest

    On 2012-02-22, Ben Morrow <> wrote:
    >
    > Quoth Justin C <>:
    >> On 2012-02-22, STD <> wrote:
    >> > 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.

    >
    > ...or you could do it the hard way :).
    >
    > (See my reply xthread.)



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

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Feb 22, 2012
    #5
  6. STD

    Tim McDaniel Guest

    In article <>,
    Justin C <> wrote:
    >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.

    --
    Tim McDaniel,
     
    Tim McDaniel, Feb 22, 2012
    #6
  7. STD

    Jim Gibson Guest

    In article <ji35u3$os4$>, Tim McDaniel
    <> wrote:

    > In article <>,
    > Justin C <> wrote:
    > >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.


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

    --
    Jim Gibson
     
    Jim Gibson, Feb 22, 2012
    #7
  8. STD

    Justin C Guest

    On 2012-02-22, Jim Gibson <> wrote:
    > In article <ji35u3$os4$>, Tim McDaniel
    > <> wrote:
    >
    >> In article <>,
    >> Justin C <> wrote:
    >> >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.

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


    That's useful, I'll try and remember that.

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Feb 24, 2012
    #8
  9. >>>>> "STD" == STD <> writes:

    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.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
    See http://methodsandmessages.posterous.com/ for Smalltalk discussion
     
    Randal L. Schwartz, Feb 26, 2012
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    326
    Fredrik Lundh
    Oct 30, 2006
  2. Replies:
    5
    Views:
    476
    Bo Yang
    Nov 2, 2006
  3. Sameer
    Replies:
    6
    Views:
    335
  4. Bal Sidhu
    Replies:
    3
    Views:
    237
    Robert Klemme
    Nov 13, 2010
  5. Sangeet
    Replies:
    7
    Views:
    225
    Sangeet
    Mar 23, 2012
Loading...

Share This Page