Good catch on the FindClose which was lost while I was quickly cutting and pasting the code sample. Probably a good idea to throw a try finally around it as well. The directory check could be leaner as well with a "if (iTest.Attr and faDirectory) = faDirectory then" as could the string compares which might bring back some time on a large C:\ search.
As for the resources as it processes through the directories thought; once the FindClose is in a directory tree would have to be extremely deep before memory became noticeably larger. With a recursive function it isn't how many but rather how deep the directory structure is. Since most directory structures are rarely more than 10-15 deep what the recursive function is holding in memory is pretty minimal. FindFirst also has an advantage over many other searching routines as it does not change the access time on a file and why many routines tend to use it over the fast scan methods. I have a multi-threaded version of this search on my modern applications that is much faster but it is also much more complex. On Thu, Feb 24, 2011 at 8:33 AM, Kyle Cordes <k...@kylecordes.com> wrote: > On Wednesday, February 23, 2011 at 3:38 PM, Cameron Cole wrote: > Recursion is the trick. Here is a partial function we use to loop through > > all the files in our project hunting for DFM files to modify (source is > old > > from Delphi3 and there are cleaner ways to do some of it): > > > > Cameron, > > I hate to throw cold water on an attempt to be helpful, but I see two > issues with your solution: > > First, it is missing the call to SysUtils.FindClose. This leaks resources > of some kind, which will eventually cause trouble. (However, it might be a > long time, and I heard a rumor somewhere that some versions of Windows will > garbage-collect file-find handles, in an attempt to work around this common > problem in application code.) > > Second, for the general case it is usually better to scan each directory in > full before scanning those inside it, for similar reasons to that above... > this avoids asking Windows to keep the file-find context for numerous > directories at once if the code is executed on a very deep directory > hierarchy. Either a stack or queue can be used (most easily, by using a > TStringList as either), to get depth-first for breadth-first search > behavior. (Of course, if you're only ever pointing it at a shallow hierarchy > looking for DFM files, this is moot.) > > > -- > Kyle Cordes > http://kylecordes.com > > __________________________________________________ > Delphi-Talk mailing list -> Delphi-Talk@elists.org > http://lists.elists.org/cgi-bin/mailman/listinfo/delphi-talk > __________________________________________________ Delphi-Talk mailing list -> Delphi-Talk@elists.org http://lists.elists.org/cgi-bin/mailman/listinfo/delphi-talk