Solved

Can I make a list of all files in a dir and subdir (without TFileListBox)?

Posted on 2003-11-09
8
200 Views
Last Modified: 2010-04-05
I need to store a File List wich match with an determinated extesion in a folder and your subfolders without use anyone visual component like OpenDialog or FileListBox.
Any Sugest???
0
Comment
Question by:NoiS
  • 4
  • 2
8 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 40 total points
ID: 9709468
a sample

procedure FindFile(PathFileName : string; IncludeSubDirs : Boolean; const ResultList : TStrings);
var
 srec : TSearchRec;
 Path, FileName : String;
begin
  If Assigned(ResultList) then
  begin
    Path := ExtractFilePath(PathFileName);
    FileName := ExtractFileName(PathFileName);
    // CurrentDirectory
    if findfirst(PathFileName,faanyfile,srec) = 0 then
    begin
      repeat
        If Not(srec.attr and fadirectory > 0) then
        begin
          //file Found->add to list
          ResultList.Add(Path+SRec.Name);
        end;
      Until findnext(srec) <> 0;
    end;
    FindClose(SRec);
    { SubDirectories }
    If IncludeSubDirs then
    begin
      if FindFirst(Path+'*.*',FaAnyFile,Srec) = 0 then
      begin
        repeat
          // StartRecursion
          if not(Srec.Name[1] = '.') and (srec.attr and fadirectory > 0)then
            FindFile(Path + Srec.Name + '\' + FileName,IncludeSubDirs, ResultList)
        Until findnext(srec) <> 0;
      end;
      FindClose(SRec);
    end;
  end
  else raise Exception.Create('No ResultList Given');
end;

//edit2 contains the starting directory
//edit1 contains a filemask (*.pas for example)
procedure TForm1.Button1Click(Sender: TObject);
begin
  if not DirectoryExists(Edit2.text) then
    raise Exception.Create('Invalid Directory');
  //check Backslash
  If Edit2.text[Length(Edit2.text)] <> '\' then
    Edit2.text := Edit2.text + '\';

  FindFile(Edit2.Text+Edit1.Text,True,ListBox1.Items);

end;

meikl ;-)
0
 
LVL 2

Expert Comment

by:monir
ID: 9709491
Hi,

Just use a recursive function using FindFirst, FindNext, and FindClose.

Monir.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9709497
monir, look above :-)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 2

Expert Comment

by:monir
ID: 9709506
sorry, I wrote my answer offline, then I sow your answer after connecting and posting.

Monir ;-)

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 9709574
happens to me too sometimes ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10225212
hello, nois, still alive?
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question