dododedodonl
dododedodonl

Reputation: 4605

How can I find all MP3 files in a directory?

I recently started with delphi and now I want to get all mp3 files from a directory. I want something like the php function glob().

Upvotes: 3

Views: 1154

Answers (5)

Bill99
Bill99

Reputation: 395

The old way of doing it is approx:

var
  status : dword;
  sr : TSearchRec;
begin
  status := FindFirst('*.mp3',faAnyFile,sr);
  while status = 0 do
  begin

     // sr.Name is the filename; add it to a list
     // or something. Note there is no path so you
     // may need to add that back on somewhere

     status := FindNext(sr);
  end;
  SysUtils.FindClose(sr);

  // ...
end;

Upvotes: 12

Jeroen Wiert Pluimers
Jeroen Wiert Pluimers

Reputation: 24463

The ancient TFileListBox in the Delphi FileCtrl unit is a good solution.

It has been there since Delphi 1, and About Delphi has a nice example on how to use it.

You can drop it on a form, set Visible = False, and never worry about it.

It supports filtering (for instance on extension), so it will work very nicely with your *.mp3 criterion.

--jeroen

Upvotes: 1

Logman
Logman

Reputation: 654

a very nice free component is TFindFile at Delphi Area: http://www.delphiarea.com/products/delphi-components/findfile/

it will give you complete control over searching for files/folders, threaded or non

Upvotes: 0

mjn
mjn

Reputation: 36634

If additional libraries are ok for you, take a look at the Jedi Code Library.

In unit common\JclFileUtils, there is a compact helper function:

function BuildFileList(const Path: string; 
  const Attr: Integer; const List: TStrings; 
  IncludeDirectoryName: Boolean = False): Boolean;

The JCL is well maintained and includes great extensions and some IDE improvements. The (very easy to use) JCL installer is available at http://sourceforge.net/projects/jcl/

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 612794

Try IOUtils.TDirectory.

Upvotes: 3

Related Questions