Article View: borland.public.cppbuilder.nativeapi
Article #15406Re: How to find files in subfolders?
From: Michael Kelly
Date: Fri, 04 Jul 2003 14:41
Date: Fri, 04 Jul 2003 14:41
95 lines
3610 bytes
3610 bytes
On 4 Jul 2003 06:52:20 -0700, "Mirko Rajkovaca" <mirko796@ptt.yu> wrote:
>
>I know for that way but I was wondering is there some "easier" way to do it.
>Thank you anyway!
Unfortunately there's no API that walks a directory tree.
Here's one I ported from Python to Delphi. You
may be able to compile it with the BCB compiler. I don't
have BCB to try it but BCB can compile some Delphi
code. If not, then yourself or someone who can read
Delphi should be able to port it to BCB.
What it does is recursively work down a directory
tree creating a TStringList of all the regular files
it finds in each directory. WalkTree calls the procedure
'dirproc' on each TStringList of filenames. The idea
is that the dirproc you write should take the root
of the tree(top) a pattern for what filenames to
match(arg) and the TStringList for the list of
filenames, so you can do whatever to all matching
files.
It's only 60 lines of code:
Unit walk;
Interface
Uses Sysutils, classes, FileCtrl;
Type
TDirProc = Procedure(dir, arg: String; filenames: TStringList);
// ListDir creates a TStringList of filenames and returns it
// so if not Nil the TStringList should be freed by the caller
Function ListDir(dir: String): TStringList;
// Call WalkTree passing in some procedure to process
// all files in TStringList matching 'arg'
Procedure WalkTree(top, arg: String; dirproc: TDirProc);
Implementation
Function ListDir(dir: String): TStringList;
Var
sr: TSearchRec;
FileName : String;
Begin
Result := TStringList.Create;
If Result = Nil Then
Exit;
Try
FileName := IncludeTrailingBackslash(dir) + '*.*';
If FindFirst(FileName, faAnyFile, sr) <> 0 Then
Exit;
Repeat
Result.Add(sr.Name);
Until FindNext(sr) <> 0;
Finally
FindClose(sr);
End;
End;
Procedure WalkTree(top, arg: String; dirproc: TDirProc);
Var
names: TStringList;
Name: String;
x: Integer;
Begin
Try
names := ListDir(top);
If (names = Nil) Or (names.Count = 0) Then
Exit;
dirproc(top, arg, names);
For x := 0 To names.Count - 1 Do
If (names[x] <> '.') And (names[x] <> '..') Then
Begin
Name := IncludeTrailingBackslash(top) + names[x];
If DirectoryExists(Name) Then
WalkTree(Name, arg, dirproc);
End;
Finally
names.Free;
End;
End;
End.
--
"I don't want to belong to any club that would have me as a member."
-- Groucho Marx
Technical Details
Message-ID:
<jh4bgv0ehkgcm8lb53ddegaf33tlipdklf@4ax.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!newshosting.com!news-xfer2.atl.newshosting.com!yellow.newsread.com!netaxs.com!newsread.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3f049d9c$1@newsgroups.borland.com> <3f04bb44@newsgroups.borland.com> <3f058694$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,9964ff4b60bae9a9 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-07-04 08:09:45 PST From: Michael Kelly <mkelly2002@adelphia.net> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: How to find files in subfolders? Date: Fri, 04 Jul 2003 10:41:56 -0400 Message-ID: <jh4bgv0ehkgcm8lb53ddegaf33tlipdklf@4ax.com> References: <3f049d9c$1@newsgroups.borland.com> <3f04bb44@newsgroups.borland.com> <3f058694$1@newsgroups.borland.com> X-Newsreader: Forte Agent 1.92/32.572 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 68.68.251.232 X-Trace: newsgroups.borland.com 1057331222 68.68.251.232 (4 Jul 2003 08:07:02 -0700) Lines: 95 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-09!supernews.com!newshosting.com!news-xfer2.atl.newshosting.com!yellow.newsread.com!netaxs.com!newsread.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail