Searching for a file - in a tree?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I wonder, which way to do this fastest.

I have a disk, where I need to search for a file or directory.

I do it recursively, meaning that I start from the top dir, then I add
all directories to an array, and by a counter I work my way through
that array.
And, while doing that I add the directory or file (name) to and result
array, if it matches.

Any better ideas?

FYI: there are 1312 folders in the system :-) App 1G of files.

My code:

      if($handle = opendir($dir))
        while (false !== ($file = readdir($handle)))
          if(($file!="..") && ($file!="."))    // avoid top dirs
            if( (($d==-1)&&is_dir("$topdir/$file")) ||
                $dirs[] = "$dirs[$d]/$file";
                $dirs[] = "$file";
              if($search && (stristr($file, $searchitem)!==false))
                  $dirs2[] = "$dirs[$d]/$file";
                  $dirs2[] = "$file";
              if(!$search || (stristr($file, $searchitem)!==false))
                  $files[] = "$dirs[$d]/$file";
                  $files[] = "$file";
    while(!$timeout && ($d<count($dirs)));

Re: Searching for a file - in a tree?

jodleren wrote:
Quoted text here. Click to load it

This isn't recursive - it's simply a loop.

I do it recursively, but check each directory as I come across it.
Pseudocode which finds a single entry.

function find_file(filename, startdir, flist = array())
   Open startdir
   While reading an entry is true
     If it's '.' or '..'
     If filename matches found entry
       Add startdir to flist
     If it's a directory
       call find_file with parameters filename, read directory and flist
           (this is the recursion call)
   End of loop
   return flist
End of function

Of course, there are other ways - but rather than keep track of all of
those directories, just scan them as you find them.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Searching for a file - in a tree?


Quoted text here. Click to load it

That's no recursion, but iteration.

What are you searching for actually and what do you want to get as a
result? PHP 5 offers a very nice way for iterating through directories
or many other kinds of list-like data structures:

$di = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($di) as $item) {
  if ($item->isFile()) {

This simple loop runs through an entire directory tree starting from the
directory given in the $path variable and returns every found item.
Inside the loop you can do whatever you want with them, there are
various methods available to get the name of the item, the full path,
the type, size, date etc.

Standard PHP Library (SPL) Functions


Re: Searching for a file - in a tree?

Quoted text here. Click to load it

I suspect that the built-in OS commands would work much faster with a
large dir tree:

On Unix:
$found=`find $topdir -name $file_to_find`;

On MS:
$found=`dir $file_to_find /s`;
// but you'll have to some parsing on the output


Site Timeline