
1arryb
User
Jun 19, 2009, 1:31 PM
Post #5 of 7
(487 views)
|
|
Re: [adamjazz1] Searching Directories for Files.
[In reply to]
|
Can't Post
|
|
Hi adam, I don't much care for opendir...readdir. Maybe someone else will help if you want to go that route. I, on the other had, suggest:
#!/usr/bin/perl # Script to get a list of directories from the given file and # search them for a file whose name exactly matches # the 2nd program argument. use strict; use warnings; use File::Find; my $inputFile = $ARGV[0]; my $matchName = $ARGV[1]; die "usage: $0 <input-file>" unless $inputFile and $matchName; open (my $in, "<", $inputFile) or die "Can't open $inputFile for reading"; while (my $dir = <$in>) { chomp($dir); unless (-d $dir ) { warn "$dir does not exist. Skipping..."; next; } # $_ has the base filename, $File::Find::name has the full pathname of the current file. # Note: You could easily have the sub {} populate a list # of matching filenames for post processing if you need # to do more than just print the names. find( sub {print $File::Find::name, "\n" if $_ eq $matchName}, ($dir)); } close $in Cheers, Larry
|