put all 559 records into a cursor TheKeys use adir() to get all the filenames and put them into a cursor TheFiles select TheFiles scan lcFileContent = filetostr( TheFiles.FileName ) select TheKeys scan for empty( FileName ) && only look for keys that haven't been found yet if ( TheKeys.Key $ lcFileContent ) replace TheKeys.Filename with TheFiles.FileName exit endif endscan endscanThis way you are reading each file only once and seeking each key in memory. This assumes the key can be found anwhere inside the file. If the files actually have some structure that could be parsed I'd build a table of filename and key columns, then you could use a single SELECT to find the files for each key.