Solved

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

Posted on 2003-11-09
8
204 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…

831 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