What edits should be made to the code in order to search only in the current directory without subdirectories?

procedure Scaserch(StartFolder, Mask: string; List: TStrings; ScanSubFolders: Boolean = True); var SearchRec: TSearchRec; FindResult: Integer; begin List.BeginUpdate; try StartFolder := IncludeTrailingBackslash(StartFolder); FindResult := FindFirst(StartFolder + '*.*', faAnyFile, SearchRec); try while FindResult = 0 do with SearchRec do begin if (Attr and faDirectory) <> 0 then begin if ScanSubFolders and (Name <> '.') and (Name <> '..') then Scaserch(StartFolder + Name, Mask, List, ScanSubFolders); end else begin if MatchesMask(Name, Mask) then List.Add(StartFolder + Name); end; FindResult := FindNext(SearchRec); end; finally FindClose(SearchRec); end; finally List.EndUpdate; end; end; 

I connect this way:

 Scaserch(SelectedFolder, '*.txt', ListBox1.Items); 
  • 2
    We need to figure out what makes this code scan subdirectories, and remove it. - MBo

1 answer 1

Your function has a ScanSubFolders parameter that controls whether a subfolder will be searched or not. By default, the search in subfolders is enabled, to disable it - pass the fourth parameter to False :

 Scaserch(SelectedFolder, '*.txt', ListBox1.Items, False); 
  • Wonderful answer, 20 minutes I could not understand what was happening. Works, respect you zed. - MaksimGurov 1:24 pm