Solved

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

Posted on 2003-11-09
8
197 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtualstringtree carsh after invalidate for while 5 86
Thread safe  opinion 7 126
Correct Component for Shopping Cart. 2 95
Making delphi communicate with a c# service 16 88
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…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

912 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now