Solved

Directory Listing

Posted on 2000-04-19
4
193 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
[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
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
mullet_attack earned 50 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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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