Solved

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

Posted on 2003-11-09
8
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 111
Run video youtube webbrowse 10 74
Graphics32 under Delphi 10.1 Berlin 2 128
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 65
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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