473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Searching for a file - in a tree?

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
4 1576
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
..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
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
..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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: pyguy | last post by:
Hi all, I am running into a conceptual glitch in implementing a simple binary tree class. My insertion and printing (sorting) seems to be ok, but when I search the tree, my find method isn't doing...
6
by: yourtrashhere | last post by:
So basically, what I have is a bunch of words in one memo field, for example: dog cat cowboy tree flower To search it, this is the code I have now. ' Check for LIKE Last Name If...
8
by: sandeep | last post by:
Our team is developing proxy server(in VC++)which can handle 5000 clients. I have to implement cache part so when ever a new request com from client I have to check the request URL content is in...
29
by: jaysherby | last post by:
I'm new at Python and I need a little advice. Part of the script I'm trying to write needs to be aware of all the files of a certain extension in the script's path and all sub-directories. Can...
3
by: wulfkat | last post by:
Ok, I feel like such an idiot with C++...gimme C# or Java any day of the week. I have a tree program that (works) creates a tree, inorder traverses it, and then prints out messages before deleting...
15
by: Gigs_ | last post by:
Hi all! I have text file (english-croatian dictionary) with words in it in alphabetical order. This file contains 179999 words in this format: english word: croatian word I want to make...
4
by: jm.suresh | last post by:
Hi, I have a tree data structure and I name each node with the following convention: a |---aa | |--- aaa | |--- aab | |---ab |
1
by: j_depp_99 | last post by:
I would like to know what would be the best way to count the nodes accessed while searching for an item in a binary search tree. I have to keep a tally for each item I search for. I have included...
4
by: Man4ish | last post by:
Can we search a value in B Tree which is less than or greater than a given value instead of searching the value. How? Thanks in advance.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.