I don’t have a console handy and can’t test this, but what jumps at me is the double use of ‘lines whose()’. Using this construct, every line of the file is read and evaluated against the whose() condition.
In the “outer” version of this, the evaluator reads one line of the file; in the whose () condition, it is checking whether the line number of this line is equal to the maximum of the line numbers starting with "Model: ".
Suppose the file has five lines, with “Model:” on lines 1 and 5. The outer loop starts on line 1. To evaluate whether this line is the last line starting with “Model:”, the evaluator has to read each line to find the lines starting with "Model: " - the evaluator finds lines 1 and 5, determines that the current line number (1) is not the maximum (5), and then moves on to the next line in the “outer loop”, stepping to line 2.
Reading line 2, it must again determine if this line number is equal to the maximum of line numbers of lines starting with "Model: ", which again means reading every line of the file.
The result is that the there are 25 line reads required. The complexity is “n squared”. A 500 line file could incur 250,000 line reads.
Better to read the file, once, and then jump directly to a read of the one line we want. Try this one …
following texts of lasts "Model: " of lines (maximum of line numbers of lines whose (it starts with “Model:”) of it) of file “SREDKey.log” of folder “XXXXXX\SREDKey” of folder (value of variable “allusersprofile” of environment)