By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Searching for a file - in a tree?

P: n/a
Hi!

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:
$d=-1;
do
{
if($d>-1)
$dir="$topdir/$dirs[$d]";
else
$dir=$topdir;

if($handle = opendir($dir))
{
while (false !== ($file = readdir($handle)))
if(($file!="..") && ($file!=".")) // avoid top dirs
{
if( (($d==-1)&&is_dir("$topdir/$file")) ||
(($d>-1)&&is_dir("$topdir/$dirs[$d]/$file")))
{
if($d>-1)
$dirs[] = "$dirs[$d]/$file";
else
$dirs[] = "$file";
if($search && (stristr($file, $searchitem)!==false))
{
if($d>-1)
$dirs2[] = "$dirs[$d]/$file";
else
$dirs2[] = "$file";
}
}
else
if(!$search || (stristr($file, $searchitem)!==false))
{
if($d>-1)
$files[] = "$dirs[$d]/$file";
else
$files[] = "$file";
}
}
closedir($handle);
}
$d++;
}
while(!$timeout && ($d<count($dirs)));
Feb 6 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
jodleren wrote:
Hi!

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:
$d=-1;
do
{
if($d>-1)
$dir="$topdir/$dirs[$d]";
else
$dir=$topdir;

if($handle = opendir($dir))
{
while (false !== ($file = readdir($handle)))
if(($file!="..") && ($file!=".")) // avoid top dirs
{
if( (($d==-1)&&is_dir("$topdir/$file")) ||
(($d>-1)&&is_dir("$topdir/$dirs[$d]/$file")))
{
if($d>-1)
$dirs[] = "$dirs[$d]/$file";
else
$dirs[] = "$file";
if($search && (stristr($file, $searchitem)!==false))
{
if($d>-1)
$dirs2[] = "$dirs[$d]/$file";
else
$dirs2[] = "$file";
}
}
else
if(!$search || (stristr($file, $searchitem)!==false))
{
if($d>-1)
$files[] = "$dirs[$d]/$file";
else
$files[] = "$file";
}
}
closedir($handle);
}
$d++;
}
while(!$timeout && ($d<count($dirs)));
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 '..'
continue
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.
js*******@attglobal.net
==================

Feb 6 '08 #2

P: n/a
..oO(jodleren)
>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.
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
http://www.php.net/manual/en/ref.spl.php

Micha
Feb 6 '08 #3

P: n/a
On 6 Feb, 20:49, Michael Fesser <neti...@gmx.dewrote:
.oO(jodleren)
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.

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) Functionshttp://www.php.net/manual/en/ref.spl.php

Micha
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:
chdir($topdir);
$found=`dir $file_to_find /s`;
// but you'll have to some parsing on the output

C.
Feb 7 '08 #4

P: n/a
..oO(C. (http://symcbean.blogspot.com/))
>On 6 Feb, 20:49, Michael Fesser <neti...@gmx.dewrote:
>>
Standard PHP Library (SPL) Functions
http://www.php.net/manual/en/ref.spl.php

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:
chdir($topdir);
$found=`dir $file_to_find /s`;
// but you'll have to some parsing on the output
Not only that, you also have to check which OS you're on, which makes
the script less portable than with using the built-in SPL. It also
requires a system call, which might not always be allowed on shared
hosts.

If this is not an issue, then OK. But the fastest solution is not always
the best. YMMV.

Micha
Feb 7 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.