Inno Setup Iterate over [Files] section in Pascal code

一世执手 提交于 2019-12-01 08:26:26

No, you cannot iterate the [Files] section.


But you can use preprocessor to generate both the [Files] section and the Pascal Script from one list of files.

You are not very specific about your goals, so I'm showing only a rough concept.

; 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")

If you compile the script, you can see in the preprocessed file Preprocessed.iss that it generates this:

[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;

Actually you may not need to use the FileCopy for this specific purpose. You can just have the preprocessor generate multiple [Files] section entries for the same file:

; 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

Generates:

[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 will identify identical source files and will pack them only once.

You can use Check parameter to make some entries conditional.


See also this question, which is actually similar:
Access file list via script in InnoSetup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!