查找所有应用程序文件夹并在Inno Setup中安装文件

时间:2017-09-26 14:25:54

标签: inno-setup

这是与Find the path of an application, and copy a file to that directory in Inno Setup

类似的问题

我想在Inno Setup中将文件安装到用户的MATLAB文件夹中。但是根据MATLAB的版本,目录可以更改,并且根据安装的版本数量,可以有多个目标。

在Windows命令行中,可以像这样获取MATLAB可执行文件的路径:

where matlab

将输出

C:\Program Files (x86)\MATLAB\R2015b\bin\matlab.exe
C:\Program Files\MATLAB\R2017a\bin\matlab.exe

“where”的输出显示两条路径,因为安装了两个版本的MATLAB。我想复制以下文件夹中的文件:

C:\Program Files (x86)\MATLAB\R2015b\bin
C:\Program Files\MATLAB\R2017a\bin

如何做到这一点?

1 个答案:

答案 0 :(得分:1)

Inno Setup无法自行将文件安装到随机数量的目标文件夹中。

你必须用Pascal脚本编写所有代码:

[Files]
Source: "MyFile.dat"; Flags: dontcopy

[Code]

procedure ExtractFileToPathsWhereAnotherFileIs(ExtractFile: string; SearchFile: string);
var
  P: Integer;
  Paths: string;
  Path: string;
  TempPath: string;
begin
  { Extract the file to temporary location (there's no other way) }
  ExtractTemporaryFile(ExtractFile);
  TempPath := ExpandConstant('{tmp}\' + ExtractFile);

  Paths := GetEnv('PATH');
  { Iterate paths in PATH environment variable... }
  while Paths <> '' do
  begin
    P := Pos(';', Paths);
    if P > 0 then
    begin
      Path := Trim(Copy(Paths, 1, P - 1));
      Paths := Trim(Copy(Paths, P + 1, Length(Paths) - P));
    end
      else
    begin
      Path := Trim(Paths);
      Paths := '';
    end;

    { Is it the path we are interested in? }    
    if FileExists(AddBackslash(Path) + SearchFile) then
    begin
      Log(Format('Found "%s" in "%s"', [SearchFile, Path]));
      { Install the file there }
      if FileCopy(TempPath, AddBackslash(Path) + ExtractFile, False) then
      begin
        Log(Format('Installed "%s" to "%s"', [ExtractFile, Path]));
      end
        else
      begin
        MsgBox(Format('Failed to install "%s" to "%s"', [ExtractFile, Path]),
               mbError, MB_OK);
      end;
    end;
  end;
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  if CurStep = ssInstall then
  begin
    ExtractFileToPathsWhereAnotherFileIs('MyFile.dat', 'matlab.exe');
  end;
end;