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

how could I go about opening multiple directories and searching for one file

and then saving that directory which contines the file, to a variable.
  • Comment on searching multiple directories for one file

Replies are listed 'Best First'.
Re: searching multiple directories for one file
by runrig (Abbot) on May 29, 2001 at 22:54 UTC
    If you want to recursively search a directory and its subdirectories, use File::Find, if you want to search for files in just the top level of a directory or multiple directories, use opendir, readdir, and closedir or glob.
Re: searching multiple directories for one file
by ckohl1 (Hermit) on May 29, 2001 at 23:25 UTC
    #!f:/perl/bin/perl.exe use strict; my @directories = qw( c:/temp f:/temp ); my $searchFile = 'seaBiscuit.fil'; foreach my $directory ( @directories ){ opendir ( DIRHANDLE, $directory ) or die "$directory not found\n"; foreach my $directoryFile (sort ( readdir ( DIRHANDLE ) ) ) { if ( ( $directoryFile cmp $searchFile ) == 0 ) { print "\nThe file I was looking for is $directory/$directo +ryFile\n"; exit; } } } print "\nI was unable to find $searchFile.\n"; exit;



    Chris
    'You can't get there from here.'
Re: searching multiple directories for one file
by blue_cowdawg (Monsignor) on May 29, 2001 at 22:37 UTC

    Try running find2perl with the find options of your choice. For instance:

    find2perl . -depth -name 'foo.pl' -print

    Save the results to a file and edit the sub "wanted" as desired to perform whatever function you want it to.

    I've given you enough to push you in the right direction so you can come up with the rest of the solution on your own! ;-)


    Peter L. BergholdSchooner Technology Consulting, Inc.
    Peter@Berghold.Netwww.berghold.net
    Unix Professional Services