@@ -291,34 +291,69 @@ procedure TSongs.LoadSongList;
291291
292292procedure TSongs.FindFilesByExtension (const Dir: IPath; const Ext: IPath; Recursive: Boolean; var Files: TPathDynArray);
293293var
294+ DirList: TPathDynArray;
295+ DirCount, DirIdx: Integer;
294296 Iter: IFileIterator;
295297 FileInfo: TFileInfo;
296298 FileName: IPath;
297- begin
298- // search for all files and directories
299- Iter := FileSystem.FileFind(Dir.Append(' *' ), faAnyFile);
300- while (Iter.HasNext) do
299+ function CollectDirectories (const StartDir: IPath): TPathDynArray;
300+ var
301+ LocalDirs: TPathDynArray;
302+ LocalIter: IFileIterator;
303+ LocalFileInfo: TFileInfo;
304+ LocalFileName: IPath;
305+ SubDirs: TPathDynArray;
306+ SubDirCount, SubDirIdx: Integer;
301307 begin
302- // the debug statements in this function have exactly the same message length before it prints the path
303- FileInfo := Iter.Next;
304- FileName := FileInfo.Name ;
305- if ((FileInfo.Attr and faDirectory) <> 0 ) then
308+ SetLength(LocalDirs, 1 );
309+ LocalDirs[0 ] := StartDir;
310+ if not Recursive then
311+ Exit(LocalDirs);
312+
313+ // Only search for directories
314+ LocalIter := FileSystem.FileFind(StartDir.Append(' *' ), faDirectory);
315+ while (LocalIter.HasNext) do
306316 begin
307- if Recursive and (not FileName.Equals(' .' )) and (not FileName.Equals(' ..' )) and (not FileName.Equals(' ' )) then begin
308- Log.LogDebug(' Recursing: ' + Dir.Append(FileName).ToWide, ' TSongs.FindFilesByExtension' );
309- FindFilesByExtension(Dir.Append(FileName), Ext, true, Files);
317+ LocalFileInfo := LocalIter.Next;
318+ LocalFileName := LocalFileInfo.Name ;
319+ if ((LocalFileInfo.Attr and faDirectory) <> 0 ) and
320+ (not LocalFileName.Equals(' .' )) and (not LocalFileName.Equals(' ..' )) and (not LocalFileName.Equals(' ' )) then
321+ begin
322+ // Add subdirectory and recurse
323+ SubDirs := CollectDirectories(StartDir.Append(LocalFileName));
324+ SubDirCount := Length(SubDirs);
325+ if SubDirCount > 0 then
326+ begin
327+ SetLength(LocalDirs, Length(LocalDirs) + SubDirCount);
328+ for SubDirIdx := 0 to SubDirCount - 1 do
329+ LocalDirs[High(LocalDirs) - SubDirCount + 1 + SubDirIdx] := SubDirs[SubDirIdx];
330+ end ;
310331 end ;
311- end
312- else
332+ end ;
333+ Exit(LocalDirs);
334+ end ;
335+ begin
336+ // First, collect all directories (including Dir itself)
337+ DirList := CollectDirectories(Dir);
338+ DirCount := Length(DirList);
339+
340+ for DirIdx := 0 to DirCount - 1 do
341+ begin
342+ Iter := FileSystem.FileFind(DirList[DirIdx].Append(' *.txt' ), 0 );
343+ while (Iter.HasNext) do
313344 begin
314- // do not load files which either have wrong extension or start with a point
315- if (Ext.Equals(FileName.GetExtension(), true) and not (FileName.ToUTF8()[1 ] = ' .' )) then
345+ FileInfo := Iter.Next;
346+ FileName := FileInfo.Name ;
347+ if ((FileInfo.Attr and faDirectory) = 0 ) then
316348 begin
317- Log.LogDebug(' Found file ' + Dir.Append(FileName).ToWide, ' TSongs.FindFilesByExtension' );
318- SetLength(Files, Length(Files)+1 );
319- Files[High(Files)] := Dir.Append(FileName);
320- fTotalSongsToLoad := fTotalSongsToLoad + 1 ;
321- UpdateLoadingProgress;
349+ if (Ext.Equals(FileName.GetExtension(), true)) then
350+ begin
351+ Log.LogDebug(' Found file ' + DirList[DirIdx].Append(FileName).ToWide, ' TSongs.FindFilesByExtension' );
352+ SetLength(Files, Length(Files)+1 );
353+ Files[High(Files)] := DirList[DirIdx].Append(FileName);
354+ fTotalSongsToLoad := fTotalSongsToLoad + 1 ;
355+ UpdateLoadingProgress;
356+ end ;
322357 end ;
323358 end ;
324359 end ;
0 commit comments