Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Directory Listing

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
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

610 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