Recent

Author Topic: Recursive search keeps going after result found  (Read 2214 times)

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Recursive search keeps going after result found
« on: September 19, 2018, 05:53:57 pm »
I'm pulling my hair out trying to solve this one. The following code does a search through a given set of directories/folders in Windows for a specific file. I've verified with watches that it finds the file in question and sets Found to true. However, it then doesn't exit and, in the outer code, doesn't return the directory string.

Have I made some stupid and obvious error? If so, I can't see it.

Any help would be gratefully received.

Code: Pascal  [Select][+][-]
  1. function GetMatchingFiles(OurPath: string; specific: string): string;
  2. var SR: TSearchRec;
  3.     Directory, FS: string;
  4.     R: LongInt;
  5.  
  6. begin
  7.    Directory:= ExtractFilePath(OurPath);
  8.    R:= FindFirst(OurPath, $23, SR);
  9.    if R = 0 then    // Found
  10.    begin
  11.       repeat
  12.         If SR.name = specific then Found:= True;
  13.                 until Found or (FindNext(SR) <> 0);
  14.    end;  // If
  15.    If Found then
  16.    begin
  17.       Result:= Directory;
  18.       FindClose(SR);
  19.       Exit;
  20.    end;
  21.  
  22.     // Subdirectories
  23.     if not Found then
  24.     begin
  25.       if FindFirst(directory + '*.*', faDirectory, SR) = 0 then
  26.       begin
  27.          repeat
  28.          if ((SR.Attr and faDirectory) = faDirectory) and (SR.Name[1] <> '.') then
  29.              GetMatchingFiles(directory + SR.Name + '\' + ExtractFileName(OurPath), specific);
  30.          until FindNext(SR) <> 0;
  31.       end; // if
  32.     end; // If not found
  33.  
  34.   end;  //GetMatchingFiles      
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: Recursive search keeps going after result found
« Reply #1 on: September 19, 2018, 06:30:05 pm »
The following code does a search through a given set of directories/folders in Windows for a specific file.
Try to understand the following working code.
Method 1 (using FileUtil):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileFound(FileIterator: TFileIterator);
  2. begin
  3.   Caption := FileIterator.FileName;
  4.   FileIterator.Stop;
  5. end;
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. var
  9.   Searcher: TFileSearcher;
  10. begin
  11.   Searcher := TFileSearcher.Create;
  12.   try
  13.     Searcher.OnFileFound := @FileFound;
  14.     Searcher.Search('c:\windows', 'hosts');
  15.   finally
  16.     Searcher.Free;
  17.   end;
  18. end;

Method 2 (using SysUtils):
Code: Pascal  [Select][+][-]
  1. function SearchInDepth(const Where: string; const What: string): string;
  2. var
  3.   R: TSearchRec;
  4.   Path: string;
  5. begin
  6.   Path := IncludeTrailingPathDelimiter(Where);
  7.   if FindFirst(Path + '*', faAnyFile, R) = 0 then
  8.   try
  9.     repeat
  10.       if (R.Attr and faDirectory) = 0 then
  11.       begin
  12.         if SameFileName(R.Name, What) then
  13.           Exit(Path + What);
  14.       end
  15.       else
  16.         if (R.Name <> '.') and (R.Name <> '..') then
  17.         begin
  18.           Result := SearchInDepth(Path + R.Name, What);
  19.           if Result <> '' then
  20.             Exit;
  21.         end;
  22.     until FindNext(R) <> 0;
  23.   finally
  24.     FindClose(R);
  25.   end;
  26.   Result := '';
  27. end;
  28.  
  29. procedure TForm1.Button2Click(Sender: TObject);
  30. begin
  31.   Caption := SearchInDepth('c:\windows', 'hosts');
  32. end;

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Recursive search keeps going after result found
« Reply #2 on: September 19, 2018, 09:47:40 pm »
Thanks for your help, ASerge. Will look at your code tomorrow but it seems very promising.
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Recursive search keeps going after result found
« Reply #3 on: September 20, 2018, 03:46:15 pm »
I'm pulling my hair out trying to solve this one. The following code does a search through a given set of directories/folders in Windows for a specific file. I've verified with watches that it finds the file in question and sets Found to true. However, it then doesn't exit and, in the outer code, doesn't return the directory string.

Have I made some stupid and obvious error? If so, I can't see it.

Any help would be gratefully received.

Code: Pascal  [Select][+][-]
  1. function GetMatchingFiles(OurPath: string; specific: string): string;
  2. var SR: TSearchRec;
  3.     Directory, FS: string;
  4.     R: LongInt;
  5.  
  6. begin
  7.    Directory:= ExtractFilePath(OurPath);
  8.    R:= FindFirst(OurPath, $23, SR);
  9.    if R = 0 then    // Found
  10.    begin
  11.       repeat
  12.         If SR.name = specific then Found:= True;
  13.                 until Found or (FindNext(SR) <> 0);
  14.    end;  // If
  15.    If Found then
  16.    begin
  17.       Result:= Directory;
  18.       FindClose(SR);
  19.       Exit;
  20.    end;
  21.  
  22.     // Subdirectories
  23.     if not Found then
  24.     begin
  25.       if FindFirst(directory + '*.*', faDirectory, SR) = 0 then
  26.       begin
  27.          repeat
  28.          if ((SR.Attr and faDirectory) = faDirectory) and (SR.Name[1] <> '.') then
  29.              GetMatchingFiles(directory + SR.Name + '\' + ExtractFileName(OurPath), specific);
  30.          until FindNext(SR) <> 0;
  31.       end; // if
  32.     end; // If not found
  33.  
  34.   end;  //GetMatchingFiles      

Have you tried inserting a Break after Found:=True?
Code: [Select]
repeat
        If SR.name = specific then
            begin
                Found:= True;
                Break;
            End;
until Found or (FindNext(SR) <> 0);
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Recursive search keeps going after result found
« Reply #4 on: September 25, 2018, 11:01:25 am »
Thanks again, ASerge. Your second example worked fine for me. (I guess I just need to spend longer looking at examples of recursion, because I can never work out what the problem is when it goes wrong.)
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

 

TinyPortal © 2005-2018