Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File::Find

Status
Not open for further replies.

uniopp

Technical User
Oct 7, 2001
152
JP
I'm using the File::Find module to search for files in a particular sub directory on my server.
I have it working but if the directory does not exist, my script seems to stall as if it's
locked in an endless search. If it finds the sub directory but it is empty it seems to move
up a directory level and returns file names from that level.
Is this normal behavior?
Is there some code that I can add to stop the search and return an error if the directory
does not exist or is empty?
Following is a snippet of code.
Thank you.

Code:
sub get_files {
my $file;
@files=();
find (\&wanted, "/path/directory/subdirectory/");
foreach $file (@files) {
#do something
}
}


sub wanted {
  
#grab the actual file names using $_
my ($filename) = $_;   
-f and push @files, $filename;  #push only file names into an array
}
 
How about something like this:
Code:
use File::Find;
use File::Basename;

my @files;
my $path = dirname($0).'/files'; # Your path goes here
die "Aborting - $path does not exist\n" unless -d $path;

sub wanted {
    -f && push(@files, $_);
}

find({'wanted'=>\&wanted, 'no_chdir' => 1}, $path);
die "Aborting - $path is empty\n" unless scalar @files;
print "$_\n" for @files;
 
File::Find should not move up a directory, it should only move down a directory: File::Find will find sub directories but not parent directories. The thing with file::find is that it's going to go through all the sub directories of the start directory, and if there are a lot of directories and files it can take a little bit to go through them all. You can also do this:

find (\&wanted, "/path/directory/subdirectory/") || die "$!";

and the script will return an error if the start directory is not found.
 
Thanks for your help.
I will try to incorporate one of your suggested methods.
I have hit another problem when using File::Find and would appreciate your help.
If there are 23 or 25 files found in the nominated directory the following conditional
statements work but if there are 24 files found it is stalling the script and
overloading my server. I assumed that 24 should invoke the else statement as is the case with
25 but it doesn't seem to be triggering either the if or else clause.
Any ideas what I am doing wrong?
Thank you.

Code:
my @images =&get_files; #goes and gets the files using File::Find
my $number_found=@images;

if ($number_found<=23){
#do something
}
#else 
else{
#do something else
}
 
What is your script doing? If I were you, I'd take a very close look at it's logic. It sounds to me like the script is poorly written and is probably deserving of some rework.
 
My perl ability is very limited so the script is probably poorly written.
So are you saying that the conditional clause should work as it is written and the problem is probably in some of the other coding?
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top