473,769 Members | 7,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("$to pdir/$file")) ||
(($d>-1)&&is_dir("$to pdir/$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($handl e);
}
$d++;
}
while(!$timeout && ($d<count($dirs )));
Feb 6 '08 #1
4 1603
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("$to pdir/$file")) ||
(($d>-1)&&is_dir("$to pdir/$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($handl e);
}
$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(filen ame, 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*******@attgl obal.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 RecursiveDirect oryIterator($pa th);
foreach (new RecursiveIterat orIterator($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.de wrote:
.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 RecursiveDirect oryIterator($pa th);
foreach (new RecursiveIterat orIterator($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.de wrote:
>>
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
1791
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 what I thought it should. Here is the output of running my tests: >python -i trees.py ********************************************************************** File "trees.py", line 70, in __main__.BinaryTree.find Failed example:
6
1254
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 Me.txtLastName > "" Then varWhere = varWhere & " LIKE """ & Me.txtLastName & "*" * "
8
2388
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 cache of proxy and send to client if it is cache, if it is not there then it have to get data from web server and store in proxy server cache. so i am thinking to use binary tree search(or AVL tree) to search request URL content in cache if it...
29
2660
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 someone set me on the right path to what modules and calls to use to do that? You'd think that it would be a fairly simple proposition, but I can't find examples anywhere. Thanks.
3
4313
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 the tree (and printing out more messages). That all works. What doesn't work is the interface from the file to the tree. Uh, let me explain that. When I started this program, I just asked the user to input the tree from the console, easy...
15
2137
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 instant search for my gui Instant search, i mean that my program search words and show words to user as user type letters.
4
1889
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
1439
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 my search method from my program. <code/> void BinarySearchTree::find(int d) { //Locate the element bool found = false; if(isEmpty())
4
1799
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
10219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.