?
Solved

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

Posted on 2003-11-09
8
Medium Priority
?
211 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
6 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 160 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
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.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

807 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