in reply to Need help with File::Find

use File::Find; my @exts = qw( jpg gif tar gz bat txt ); my @excludes = qw( winnt cygwin ); my $ext = join '|', map{quotemeta} @exts; my $exclude = join '|', map{quotemeta} @excludes; my $base = '/cygwin'; find( sub { push @datein, $File::Find::name if m/\.(?:$ext)$/oi and $File::Find::name !~ m!/(?:$exclude)!oi; }, $base ); print "$_\n" for @datein;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Need help with File::Find
by ppm (Novice) on Feb 26, 2003 at 15:54 UTC
    Thx all for you help!

    @tachyon:

    if i want also the posibility to search for all (*.*) files and all directory. How to extend for this your script?

    thx again ;o)

      Instead of changing the code, allow the user to pass some options in. If tachyon would be so kind as to allow me to lift his code:
      use strict; use warnings; use File::Find; use Getopt::Long; use vars qw($all @datein @exts @excl $base); GetOptions( 'exts|t=s' => \@exts, 'excl|c=s' => \@excl, 'base|b=s' => \$base, 'all|a' => \$all, ); if (not $all) { @exts = qw( jpg gif tar gz bat txt ) unless @exts; @excl = qw( winnt cygwin ) unless @excl; } $base ||= '/cygwin'; my $exts = join '|', map{quotemeta} @exts; my $excl = join '|', map{quotemeta} @excl; find( sub { push @datein, $File::Find::name if !$exts or m/\.(?:$exts)$/oi and $File::Find::name !~ m!/(?:$excl)!oi ; }, $base, ); print "$_\n" for @datein;
      Now you can call the script like so:
      ./find.pl -exts=txt -exts=gif -excl=nodes -base=.
      
      or this if you want everything:
      ./find.pl -base=/ -all
      
      If specifying the extensions one at a time becomes too tedious for you, you can rewrite the code to allow the user to pass a delimited string instead. Read up on the Getopt::Long docs for more info. Additionally, check out the Unix command find.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        Hey,

        this snippet is very usefull for me .. thx a lot to you and all the people of perlmonks!

        ppm

        Hm ... sorry for new question.

        i've i want use the script by shell (getopt) or over http .. how to write this down?

        this was my last question for that ;o)

        ppm

      Change the if /\.(?:$ext)/ clause to if -f $_

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print