• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

create a filelist

Using Delphi 5
The prog must run under W2k and W98.

I have a directory c:\Templates.
There are subdirectories with files.
i.e.
c:\Templates\SubDir1\file1
c:\Templates\SubDir1\file2
c:\Templates\SubDir1\file3
c:\Templates\SubDir1\file4
c:\Templates\SubDir2\filex
c:\Templates\SubDir2\filey
and so on.
(not a deeper structure)

i need a list in a memo or so with all these Files and the complete path when i start my program.


0
tzigan
Asked:
tzigan
  • 2
1 Solution
 
intheCommented:
hi,

is this ok:

Regards Barry

procedure GatherFiles(Const Lines: TStrings; Const Dir,Match: String);
var
  Rec: TSearchRec;
  Found: Integer;
Begin
  Found:= FindFirst(Dir+'\'+Match, faAnyFile, Rec);
  While Found = 0 do
  begin
    If (Rec.Attr AND faDirectory) = 0 Then Lines.Add(Dir+'\'+Rec.Name);
    Found := FindNext(Rec);
  End;
  FindClose(Rec);
  Found:= FindFirst(Dir+'\*.*', faAnyFile, Rec);
  While Found = 0 do
  begin
    If ((Rec.Attr AND faDirectory)>0) AND
       (Rec.Name<>'.') AND
       (Rec.Name<>'..') Then GatherFiles(Lines,Dir+'\'+Rec.Name,Match);
    Found := FindNext(Rec);
  End;
  FindClose(Rec);
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
  memo1.Clear;
  GatherFiles(memo1.lines,'C:\mysql','*.*');
end;
0
 
bugrogerCommented:
Here is an example:

These two lines must be put into the
OnCreate - event from your form.

 Memo1.Clear; //is not necessary
 GetFiles('c:\templates', Memo1); //get all files in "DIR"


Procedure GetFiles(Dir : string; Memo : TMemo);
 Procedure _GetFiles(StartDir : string);
 var
  SR : tSearchRec;
  ERG : integer;
  s   : string;

 begin
  IF StartDir[Length(StartDir)]<>'\' then StartDir := StartDir + '\';

  ERG := findfirst(StartDir+'*.*', faANYFILE, SR);
  While (ERG = 0) do
  begin
   //IF it a directory then step in
   IF (SR.Attr and faDirectory > 0)and(SR.Name[1] <> '.') then _GetFiles(StartDir + SR.Name)
    else
   //else
   IF (SR.Name[1] <> '.') then
   Begin
    Memo.Lines.Add(StartDir + SR.Name);
   End;
   ERG := FindNext(SR);
   Application.ProcessMessages;
  end;
  FindClose(SR);
 end;
Begin
 _GetFiles(Dir);
End;

0
 
tziganAuthor Commented:
Thanks, I'll test it.
0
 
tziganAuthor Commented:
Great, Thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now