2017-09-26 65 views
1

這是一個類似問題Find the path of an application, and copy a file to that directory in Inno Setup查找所有的應用程序文件夾,並安裝有一個文件在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 

的輸出「其中」顯示兩個路徑,因爲安裝了兩個版本的MATLAB。我想將文件複製到以下文件夾中:

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

這怎麼辦?

回答

1

Inno安裝程序無法將文件自行安裝到隨機數目的目標文件夾。

你必須一切帕斯卡爾腳本代碼:

[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; 
相關問題