?
Solved

Directory Listing

Posted on 2000-04-19
4
Medium Priority
?
195 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

741 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