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().
-
3If you just recently started with Delphi, why are you on Delphi 6? – Mason Wheeler Nov 21 '10 at 16:17
-
3@Mason - Agreed! It's not like Harry Potter, you don't have to start way back at the beginning! – Chris Thornton Nov 22 '10 at 00:23
-
1@Mason Wheeler: because he had a copy lying around, and didn't want to spend 900 Euro's to search for mp3's? – The_Fox Nov 22 '10 at 07:41
-
@mason this is the version we have at school... Our service desk (system managers) are a bit slow... – dododedodonl Nov 22 '10 at 17:34
5 Answers
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;

- 395
- 2
- 8
Try IOUtils.TDirectory.

- 601,492
- 42
- 1,072
- 1,490
-
1
-
Then use FindFirst etc. as suggested by Bill99 - that's how TDirectory is implemented. – David Heffernan Nov 21 '10 at 16:03
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/

- 36,362
- 28
- 176
- 378
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

- 23,965
- 9
- 74
- 154
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

- 654
- 1
- 7
- 16