Inno Setup 迭代 Pascal 代码中的 [Files] 部分

Inno Setup Iterate over [Files] section in Pascal code

在 Inno Setup 脚本中,我需要将一些文件复制到多个用户定义的位置。为此,我想遍历 [Files] 部分中的源代码,并根据用户定义的设置和文件属性多次 FileCopy() 它们。

是否可以使用脚本访问 [Files] 部分中的源代码?

不,您不能重复 [Files] 部分。


但是您可以使用预处理器从一个文件列表中生成 [Files] 部分和 Pascal 脚本。

你的目标不是很具体,所以我只展示了一个粗略的概念。

; Define array of files to work with
#dim Files[2]
#define Files[0] "MyProg.exe"
#define Files[1] "MyProg.chm"

#define I

; Iterate the file array, generating one entry in Run section for each file    
[Files]
#sub FileEntry
Source: "{#Files[I]}"; DestDir: "{app}"
#endsub

#for {I = 0; I < DimOf(Files); I++} FileEntry


[Code]

procedure CopyFiles;
begin
  // Iterate the file array, generating two FileCopy calls for each file    
#sub FileCopy
  FileCopy('{#Files[I]}', 'd:\destination1\{#Files[I]}', True);
  FileCopy('{#Files[I]}', 'd:\destination2\{#Files[I]}', True);
#endsub

#for {I = 0; I < DimOf(Files); I++} FileCopy
end;

// Just for debugging purposes, outputs the preprocessed script
// so you can verify if the code generation went right
#expr SaveToFile(AddBackslash(SourcePath) + "Preprocessed.iss")

如果你编译脚本,你可以在预处理文件Preprocessed.iss中看到它生成了这个:

[Files]
Source: "MyProg.exe"; DestDir: "{app}"
Source: "MyProg.chm"; DestDir: "{app}"

[Code]
procedure CopyFiles;
begin
  FileCopy('MyProg.exe', 'd:\destination1\MyProg.exe', True);
  FileCopy('MyProg.exe', 'd:\destination2\MyProg.exe', True);
  FileCopy('MyProg.chm', 'd:\destination1\MyProg.chm', True);
  FileCopy('MyProg.chm', 'd:\destination2\MyProg.chm', True);
end;

实际上,您可能不需要为此特定目的使用 FileCopy。您可以让预处理器为同一文件生成多个 [Files] 节条目:

; Iterate the file array, generating one entry in Run section for each file    
[Files]
#sub FileEntry
Source: "{#Files[I]}"; DestDir: "{app}"
Source: "{#Files[I]}"; DestDir: "d:\destination1"
Source: "{#Files[I]}"; DestDir: "d:\destination2"
#endsub

生成:

[Files]
Source: "MyProg.exe"; DestDir: "{app}"
Source: "MyProg.exe"; DestDir: "d:\destination1"
Source: "MyProg.exe"; DestDir: "d:\destination2"
Source: "MyProg.chm"; DestDir: "{app}"
Source: "MyProg.chm"; DestDir: "d:\destination1"
Source: "MyProg.chm"; DestDir: "d:\destination2"

Inno Setup 将识别相同的源文件并将它们只打包一次。

您可以使用 Check parameter 使某些条目有条件。


又见这个问题,其实也差不多:
Access file list via script in InnoSetup