Board index » delphi » Listing the files in a directory?

Listing the files in a directory?

Hi,

I want to have a listing of all the files in a certain directory.
I did something with shellexecute a dir to a file and then
reading in the file. But this does not work very good. I get
a winoldap process (shown if you do ctrl-alt-del in win95)
 if I hide the form, which eventually eats up all my memory.
Dlgdirlist should do something similar, but there is no example
in the windows API. Instead of trying to invent the weel again
I thought I might post this as a question. So I would like an
example of Dlgdirlist, or a way to kill my shellexec without
showing it, or another way to list the files in a directory.

Kind regards,  Joost Sirag

 

Re:Listing the files in a directory?


On Mon, 14 Apr 1997 13:47:12 GMT, Joost.Si...@trendbox.nl (Joost

Quote
Sirag) wrote:
>Hi,

>I want to have a listing of all the files in a certain directory.

this was to walk a tree, or traverse directories, and thus, it
can call itself recursively, however, it will also do what you wish,
although you may want to modify it some.  also, you may look at
findfirst and find next and their return codes.  you can check if
there was a match, and other stuff....

------------------------------------------------------------------------------------

procedure Traverse(StartDir : String);
var
   ErrorCode : Integer;
    SearchRec : TSearchRec;
begin
   try
       ChDir(StartDir);  

       ErrorCode := FindFirst('*.*',faAnyFile,SearchRec);

       while ErrorCode = 0 do  
       begin
          if ((SearchRec.Name<>'.' )and(SearchRec.Name<>'..'))then
          begin
              if  (SearchRec.Attr and faDirectory>0) then
              begin
                 //
                 // Found a directory...
                 // If you want to also traverse subdirectories, then
                 // Call recursively using "Traverse(SearchRec.Name)"
                 //
              end else
              begin
                 //
                 // Found a file...
                 // File is SearchRec.Name, Do what you wish now.
                 //
              end; {if}
          end; {if}
          ErrorCode := FindNext (SearchRec);
          Application.ProcessMessages;
       end; {while}

   finally

       if (Length(StartDir) > 3) and (StartDir[2] <> ':') then
       begin
          //
          // Finished with directory...
          // if you also want to process the directory name,
          // Move up with "ChDir('..')"
          // and do what ever to "StartDir"
          //
       end; {if}

   end; {try}
end; {traverse}

Other Threads