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.
function GetMatchingFiles(OurPath: string; specific: string): string;
var SR: TSearchRec;
Directory, FS: string;
R: LongInt;
begin
Directory:= ExtractFilePath(OurPath);
R:= FindFirst(OurPath, $23, SR);
if R = 0 then // Found
begin
repeat
If SR.name = specific then Found:= True;
until Found or (FindNext(SR) <> 0);
end; // If
If Found then
begin
Result:= Directory;
FindClose(SR);
Exit;
end;
// Subdirectories
if not Found then
begin
if FindFirst(directory + '*.*', faDirectory, SR) = 0 then
begin
repeat
if ((SR.Attr and faDirectory) = faDirectory) and (SR.Name[1] <> '.') then
GetMatchingFiles(directory + SR.Name + '\' + ExtractFileName(OurPath), specific);
until FindNext(SR) <> 0;
end; // if
end; // If not found
end; //GetMatchingFiles