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

Hi All, I'm a beginner in perl. I'm using package find::file to traverse across directories and i need some help on how to filter directories. My requirement is to find certain files with same extension(e.g .pl) that might be present in different directories. So I want to filter out the directories based on the presence of the files that i'm searching for. Any sample code to do this will be of great help.
  • Comment on usage of find::file and glob to filter out directories and retrieve selected files

Replies are listed 'Best First'.
Re: usage of find::file and glob to filter out directories and retrieve selected files
by Nikhil Jain (Monk) on Apr 25, 2011 at 04:59 UTC

    See Beginners guide to File::Find

    see one more small example :-

    #!/usr/bin/perl -w use strict; use File::Find; print "Enter the directory to start searching in: "; chomp(my $dir = <STDIN>); # find takes a subroutine reference and the directory to start working + from. find (\&wanted, $dir); sub wanted { if(/\.pl$/) { # See if it’s a .pl file print "$File::Find::name\n"; # Print the current file name. } }

    For each file found, certain variables are set.
    • $_ is set to the name of the current file.
    • $File::Find::dir is set to the directory that contains the file.
    • $File::Find::name contains the full name of the file, i.e. $File::Find::dir/$_.

    Also have a look at File::Find::Rule- Alternative interface to File::Find

Re: usage of find::file and glob to filter out directories and retrieve selected files
by Anonymous Monk on Apr 25, 2011 at 06:31 UTC
    My requirement is to find certain files with same extension(e.g .pl) that might be present in different directories. So I want to filter out the directories based on the presence of the files that i'm searching for.

    Are you looking for files or directories? If you find the files you're looking for, there is no need to do any kind of filtering of directories

    Shell commands

    md tmp3 md tmp3\a md tmp3\a\a md tmp3\a\b md tmp3\a\c md tmp3\a\d md tmp3\b\a md tmp3\b\b touch tmp3\a\b\f.pl touch tmp3\a\c\f.pl touch tmp3\b\b\f.pl findrule tmp3 -file ( *.pl ) perl -MFile::Find::Rule -le " print for File::Find::Rule-> file( q(*.p +l) )->in( q(tmp3) ); " perl -MFile::Glob=:glob -le " while(@ARGV){ print for glob shift } " " +tmp3/*pl" "tmp3/*/*pl" "tmp3/*/*/*pl"
    Shell session
    $ md tmp3 $ md tmp3\a $ md tmp3\a\a $ md tmp3\a\b $ md tmp3\a\c $ md tmp3\a\d $ md tmp3\b\a $ md tmp3\b\b $ touch tmp3\a\b\f.pl $ touch tmp3\a\c\f.pl $ touch tmp3\b\b\f.pl $ findrule tmp3 -file ( *.pl ) tmp3/a/b/f.pl tmp3/a/c/f.pl tmp3/b/b/f.pl $ perl -MFile::Find::Rule -le " print for File::Find::Rule-> file( q(* +.pl) )->in( q(tmp3) ); " tmp3/a/b/f.pl tmp3/a/c/f.pl tmp3/b/b/f.pl $ perl -MFile::Glob=:glob -le " while(@ARGV){ print for glob shift } " + "tmp3/*pl" "tmp3/*/*pl" "tmp3/*/*/*pl" tmp3/a/b/f.pl tmp3/a/c/f.pl tmp3/b/b/f.pl
    I quoted "tmp3/*/*/*pl" because bash/csh/sh... will glob for you (cmd.exe doesn't).

    findrule, File::Find::Rule, File::Glob