What about using this ready-to-use function:
function GetFileSomewhere(FileName : String; Path : String) : string;
var
SearchRec: TSearchRec;
I : Integer;
NextPath : String;
begin
// assume failure
Result := '';
// adds '\' to Path
Path := Trim(Path);
if Path[Length(Path)] <> '\' then
Path := Path + '\';
// tries to find file on current directory
if FindFirst(Path + Filename, faAnyFile, SearchRec) = 0 then
// gets out if OK
Result := Path + Filename
else
begin
// searches all subdirectories
sysutils.FindClose(SearchRec);
I := FindFirst(Path + '*.*', faDirectory, SearchRec);
// if Result <> '' then stops, otherwise searches in all subdirectories
while (I = 0) and (Result = '') do
begin
// on each subdirectory searches for files which are not '.' or '..'
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
NextPath := Path + SearchRec.Name + '\';
Result := GetFileSomewhere(Filename, NextPath);
// if file found, set Path
if Result <> '' then
Path := NextPath;
end;
I := FindNext(SearchRec);
// processes the messages
Application.ProcessMessages;
end;
sysutils.FindClose(SearchRec);
end;
// if file not found, clears Path
if Result = '' then
Path := '';
end;