Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find folders with FindFirst/FindNext? 1

Status
Not open for further replies.

ZeeNer

Programmer
Jun 2, 2002
38
CA
Hi all,
It's me again ;P
I am trying to do a search of all the *.mp3 we can found in a specified path AND also in all the folders who are in that specified path... but with findfirst how can we search for folders? or if there's an other way to do it i'll be happy to know this way =)
 
I make so many tray with faDirectory...
to verify if faDirectory is in the Attr...
But it seem like it don't want to work correctly, i'll paste the part of my procedure here , maybe it'll help you to undestand easyly the problem :
i := FindFirst(lstDirectory.items.strings[Ind1]+'\*.*',faDirectory , w);
while i = 0 do
begin
if ((w.Attr and faDirectory) > 0) then
begin
e := FindFirst(lstDirectory.items.strings[Ind1]+w.name+'\*.mp3', $3f, z);
while e = 0 do
begin
Inc(NbMp3);
Mp3 := z.Name +' ('+floattostrF((z.Size / 1024) / 1024,ffNumber,2,2)+' meg)';
Writeln(ficListe, mp3);
e := FindNext(z);
end;
end;

all i want to do is verify if the 'file' is a folder... then if yes go in and check all the *.mp3 in it to do some operations... and do the same thing with all the folder in the specified path... I'm sure it's not a big problem here... but i need ur help ! Thx
 
It's best to build a recursive function. Below I demonstrate a function that searches a specified folder and all sub folders for mp3 files. It's recursive, hence, it calls itself to process subfolders.

And no, I'm not using highlights in every post ;-)

You can call this function like this:
FindFiles( 'c:\media\mp3\' );

procedure TForm1.FindFiles( Folder: string );
var
  FoundFile: TSearchRec;
  FindResult: Integer;
  Filename: string;
begin
  // Delphi 6 apps should use IncludeTrailingPathDelimiter
  Folder := IncludeTrailingBackSlash(Folder);
  Listbox1.Items.Add( 'Adding folder: ' + Folder );
  ListBox1.Update;

  FindResult := FindFirst(folder + '*.*', fadirectory, FoundFile);
  // We're going to find all subfolders first
  while FindResult = 0 do
  begin
    Filename := Folder + FoundFile.Name;
    // We skip the special folders '.' and '..'
    if (FoundFile.Name <> '.') and (FoundFile.Name <> '..') then
  // If a subfolder is found, process it also
  FindFiles(Filename);
    FindResult := FindNext(FoundFile);
  end;
  FindClose(FoundFile); //Forgotton too often

  //Next, we're going to find all mp3 files
  FindResult := FindFirst(Folder + '*.mp3', faAnyFile - fadirectory, FoundFile);
  while FindResult = 0 do
  begin
    ListBox1.Items.Add(Folder + FoundFile.Name);
    FindResult := FindNext(FoundFile);
  end;
  FindClose(FoundFile); //Forgotton too often
end;
 
When executing the code I found that none of my Web Site directories and files are listed. Why could this be?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top