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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looking in directory for file

Status
Not open for further replies.

mikedaruke

Technical User
Joined
Mar 14, 2005
Messages
199
Location
US
All,

What is the easiest way to run threw a directory which has 100s of sub directorys which has 100s of more sub directorys which have 100s of files. I then want to read each file and see if it has something i am looking for.

Is there a module for this or should I write something from scratch.

Currently researching so any help would be appreciated.
 
Let me also add this is a Solaris Server that I have to telnet to since I will be running the script on my personal computer.
 
File::Find can run through all the sub directories of a directory, but you still need to write the bits of code that look through the files to find what you want:



but you would be much better off using a real database for this type of intensive searching or creating an index that gets searched instead of the files.
 
If you don't want to deal with modules, you can write a recursive directory scanning subroutine:

Code:
my &scanDir ("C:/Program Files");

sub scanDir {
   my $search = shift;

   # open $search
   opendir (DIR, $search);

   # scan its contents
   foreach my $file (sort(grep(!/^\./, readdir(DIR)))) {
      # if this file is another folder...
      if (-d "$search/$file") {
         # scan this directory too!
         &scanDir ("$search/$file");

         # $search might be "C:/Program Files"
         # $file might be "Windows Messenger"
         # so it calls &scanDir ("C:/Program Files/Windows Messenger");
      }
      else {
         # This file is a normal file.
         open (FILE, "$search/$file");

         # do whatever you want on it here
         # push it onto a global array?

         close (FILE);
      }
   }

   # close the directory
   closedir (DIR);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top