Dear ALL,
In my application, I want to check if the R environment for statistical computing and graphics is installed on the computer.
To achieve this, I need to search for the R executable (R on Linux or R.exe on Windows). On Linux, R is usually installed in '/usr/lib/R', but on Windows the situation becomes more complicated (of course!) because each R installation creates a separate subdirectory under 'C:\Program Files\R', based on the installed version number (so, for example, if I have R 3.6.1 installed, the binary executable will be in 'C:\Program Files\R\R-3.6.1\bin'. I cannot know in advance what version of R a user will have in her/his computer, therefore a file serach for R.exe should start at ''C:\Program Files\R' and go down the subdirectories until the binary executable is found.
The FileSearch function in the SysUtils unit seems not to work because as far as I could understand, it does not perform recursive subdirectories searches.
I then attempted to use the FindAllFiles function which is supposed to be in the FileUtil unit (
https://wiki.freepascal.org/FindAllFiles):
program Test;
{$MODE OBJFPC}
{$APPTYPE CONSOLE}
uses Classes, SysUtils, FileUtil;
const
{$ifdef unix}
FN = 'R';
P = '/usr/lib/R';
{$else}
FN = 'R.exe';
P = 'C:\Program Files\R';
{$endif}
var
APath, AMask: string;
AList: TStringList;
i: Integer;
begin
//Writeln ('R is in : ', FileSearch(FN, P));
AList := TStringList.Create;
try
APath := P;
AMask := FN;
FindAllFiles(AList, APath, AMask, True, faDirectory);
WriteLn('Files found = ', AList.Count);
for i := 0 to AList.Count - 1 do
WriteLn(AList.Strings[i]);
finally
AList.Free;
end.
But then I got an error message from the compiler, saying that it can't find unit FileUtil. has this unit been removed from FPC/Lazarus?
Anyway, could someone give me some hnelpful insights toward solving the problem of searching recursively for a given file?
Thanks in advance!
Best regards,