Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Directory Listing

Posted on 2000-04-19
4
Medium Priority
?
199 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 200 total points
ID: 2733283
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
ID: 2733490
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
ID: 2736532
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
ID: 2749667
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

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!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

810 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