Home > Archive > PERL CGI Beginners > November 2006 > Re: File::Find
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
|
Hello
>
> I am using File::Find to find files located at a $dir directory,
> however it goes through all subdirectories. I mean I don't want
> to search recursively.
>
> Also which module you recommend for search and print through the
> files that File::Find will find.
>
> Thanks for help
> Adam
>
| |
|
| In article <456AF300.1060401@unixplanet.biz>,
perl@unixplanet.biz (Perl) wrote:
[color=darkred]
> Hello
I, too, have found a use for find at one level only because of the other
tests it can do. I have an alias in csh/tcsh:
find . \( -type d ! -name '.' -prune \) -o -print
which only works in the current directory as written. I ran this through
find2perl, and it produced:
#! /usr/bin/perl
use strict;
use File::Find ();
# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.
# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;
sub wanted;
# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '.');
exit;
sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);
(
(($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
-d _ &&
! /^\.\z/s &&
($File::Find::prune = 1)
)
||
print("$name\n");
}
If you have gnu find, then it has maxdepth and mindepth, which may
produce a different perl version. Oops - just tried that on my linux
system, and it gave an error from find2perl: "Unrecognized switch:
-maxdepth ". Oh well...
Boyd
| |
| Zentara 2006-11-28, 6:55 pm |
| On Mon, 27 Nov 2006 10:15:28 -0400, perl@unixplanet.biz (Perl) wrote:
[color=darkred]
Here's one for linux only ( because of the slashes direction)
File::Find::Rule is better.... see below
#!/usr/bin/perl
# linux only
use warnings;
use strict;
use File::Find;
use File::Spec;
if (@ARGV < 2){print "Usage: $0 dir depth\n";exit}
my ($path, $depth)= @ARGV;
my $abs_path = File::Spec->rel2abs($path); #in case you enter . for dir
my $m = ($abs_path) =~ tr!/!!; #count slashes in top path
find (\&found,$abs_path);
exit;
sub found{
my $n = ($File::Find::name) =~ tr!/!!; #count slashes in file
return $File::Find::prune = 1 if $n > ($m + $depth);
# return unless -d; # for dirs only
# do stuff here.
#print "$_\n"; #name only
print "$File::Find::name\n"; #name with full path
}
__END__
########################################
#######3
but it may be better to use File::Find::Rule
#!/usr/bin/perl
use File::Find::Rule;
@directory = qw(.);
$word = shift;
my @files = File::Find::Rule
->file()
->grep(qr/\Q$word\E/)
->in(@directory);
print "@files\n";
__END__
########################################
#########
or to include searching
#!/usr/bin/perl
use warnings;
use strict;
use File::Find::Rule;
my $word = shift || 'perl';
# find all the files of a given directory
my $directory = shift || '/home/zentara';
my $depth = shift || 3;
my $rule1 = File::Find::Rule->new;
$rule1->maxdepth($depth);
$rule1->file;
$rule1->grep(qr/\Q$word\E/); #search
#$rule->name( '*.pm' ); #match names
my @files = $rule1->in($directory);
print "@files\n";
__END__
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
|
|
|
|
|