Solved

Directory Listing

Posted on 2000-04-19
4
187 Views
Last Modified: 2008-02-01
Hi

I want to get the filenames in a directory using the WinApi functions. I know that it can be done with a recursive function/procedure but I'm unable to get the desired effect using Delphi. My code reads the files in the root and then in one subdirectory ad then exists. I've done this in VB but am unable to get it to work in Delphi.

There are likely to be subdirectories of subdirectories so I really need to get recursive code.

Please send me some code as I'm still new to Delphi.
0
Comment
Question by:jfvd
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
mullet_attack earned 50 total points
Comment Utility
Hi jfvd,

put a button and a memo on a form :
the memo will list all the dirs on your c drive. "SearchDisk" is recursive..

procedure TForm1.Button1Click(Sender: TObject);
begin
searchdisk('c:');
end;

procedure TForm1.SearchDisk(drive: string);
var
  sr: TSearchRec;
  FileAttrs: Integer;
  Path : string;
  Save_Cursor:TCursor;
begin
  Save_Cursor := Screen.Cursor;
  Screen.Cursor := crHourglass;
  FileAttrs := faDirectory;
  try
    Path := drive;
    memo1.lines.add(Path + '\');
    Application.ProcessMessages;
    if FindFirst(Path + '\*.*', FileAttrs, sr) = 0 then
      begin
        begin
          if (sr.Attr and FileAttrs) = sr.Attr then
          begin
            if ((sr.name <> '.') and (sr.name <> '..')) then
              SearchDisk(Path + '\' + sr.name);
          end;
          while (FindNext(sr) = 0) do
          begin
            Application.ProcessMessages;
            if (sr.Attr and FileAttrs) = sr.Attr then
            begin
            if ((sr.name <> '.') and (sr.name <> '..')) then
              SearchDisk(Path + '\' + sr.name);
            end;
          end;
          FindClose(sr);
        end;
      end;
   finally
     Screen.Cursor := Save_Cursor;
   end;
end;

0
 

Author Comment

by:jfvd
Comment Utility
Thanx I'll try it out and let you know. The code doesn't seem to read the filenames as well, only the directory names.

Fick
0
 
LVL 2

Expert Comment

by:mullet_attack
Comment Utility
You're right, it does only read directories. I offered the code as a sample of recursion only. If you want to see files, just change the line FileAttrs := faDirectory to FileAttrs := faDirectory + faAny.
0
 

Author Comment

by:jfvd
Comment Utility
Thanx for your trouble. I've eventualy found why my code did not want to work - I declared my variable to TSearchRec in the private part of the type declaration and not in the procedure body, the moment I changed that even my code works.

Thanx a lot
 Fick
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

10 Experts available now in Live!

Get 1:1 Help Now