• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1809
  • Last Modified:

Getting a directory listing and storing in a listbox - Delphi

I need to figure out how to return a list of directories available under a specific path.

Example:
C:\JukeBox\Music\ (starting directory)
under this directory are directories of artists (ex. \Metallica\)
under the artist directory are directories of albums (ex. \Ride The Lightning\)

What I'm looking to do is grab a list of these directories somehow and store them in a listbox to iterate through them as needed.
Example of intended listbox contents:
C:\JukeBox\Music\Metallica\Ride The Lightning\
C:\JukeBox\Music\Metallica\Death Magnetic\
C:\JukeBox\Music\Megadeth\Rust In Peace\
C:\JukeBox\Music\Elvis\Greatest Hits\
ect....

The app currently uses a findfile function which recursively searches for .mp3 files and it grabs the filepath from the mp3. This is not very efficient and will really bog down once the mp3 collection gets larger.

Please help! :) I'm using Delphi 7

Thanks,
Jason
0
jasonkrueger
Asked:
jasonkrueger
2 Solutions
 
JohnjcesCommented:
You would use a FindFirst routine such as...

var
 Rec  : TSearchRec;

if FindFirst(Path, faDirectory, Rec) = 0 then
begin
// Your stuff here
  if ((Rec.Attr and faDirectory) <> 0)  and (Rec.Name<>'.') and (Rec.Name<>'..') then
  begin
   To iterate through subdirectories, your stuff goes here.
  end;
end;

This is pretty rough off the top of head, but if you need a complete example, let me know. Code abounds to iterate directories and instead of using FindFile, you should look at a FindFirst(Path, faAnyFIle, Rec) = 0.

Hope this helps.

JOhn
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
procedure TForm1.ListDirectory(Start: String; List: TListBox);
var
   Res: TSearchRec;
   EOFound: Integer;
begin
   EOFound := FindFirst(Start, faDirectory, Res);
   while EOFound = 0 do begin
     if ((Res.Attr and faDirectory) = fadirectory) and
        (Res.Name[1] <> '.') and (Res.Name[1] <> '..') then
      begin
        List.items.Add(ExtractFilePath(start)+Res.Name);
        ListDirectory(ExtractFilePath(start)+Res.Name+'\*.*',List);
      end;
       EOFound:= FindNext(Res);
     end;
   FindClose(Res) ;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListDirectory('C:\JukeBox\Music\*.*',ListBox1);
end;
0
 
jasonkruegerAuthor Commented:
thanks for the replies. sorry could test these right away. both solutions work.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now