Finding a file by name in a directory tree

Posted by Peter Judge on 10-Feb-2009 08:48

Hi,

I'm looking for a - no, make that the fastest - way to search a directory and its children for a file by name (file name only, no path). The file may or may not exist. I'm written this sort of code before, and it works just fine, but in this case I'm quite concerned about performance, given that it will happen during session bootstrapping.

Broadening the question somewhat, this work is required if I choose to use the Convention over Configuration pattern (which is what it sounds like: instead of using configuration files like XML or INI or even PF, I guess, an application looks for known patterns in known places or with known names). My personal tendency is for the configuration approach but I can definitely see the benefits of using the convention approach, especially from an ease-of-use perspective. I'm wondering if anyone has any experience on one over the other.

Thanks,

Peter

All Replies

Posted by Peter Judge on 10-Feb-2009 09:57

I'm looking for a - no, make that the fastest - way

to search a directory and its children for a file by

name (file name only, no path). The file may or may

not exist. I'm written this sort of code before, and

it works just fine, but in this case I'm quite

concerned about performance, given that it will

happen during session bootstrapping.

I'm clearly getting on in years .... a colleague pointed out to me that I've done this sort of thing before. The approach I took then was to build a full path and do a SEARCH() on that. This is really fast and did the trick.

-- peter

Posted by Tim Kuehn on 11-Feb-2009 09:43

if the directory is relatively static, my directory tools code does a good job of reading a complete directory tree and returning a TT that can then be searched. It has a lot of other options that can be used to do all sorts of interesting things as well.

See the code share area for the code itself.

Message was edited by:

Tim Kuehn

Posted by Peter Judge on 11-Feb-2009 09:57

This looks pretty cool. I have 2 enhancement suggestions:

1) allow CLS files to be compiled, and

2) allow multiple file masks. My current use-case looks for matches on 2 masks in the same tree. I don't want to have to traverse the tree more than once if I can help it.

-- peter

Posted by Tim Kuehn on 11-Feb-2009 10:24

I may look at 'class'ifying this in the future, and these are things that can be looked at then.

Glad you liked the tool!

This thread is closed