Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inno Setup Iterate over [Files] section in Pascal code

In an Inno Setup script, I need to copy a number of files to multiple user-defined locations. In order to do this, I'd like to iterate over the sources in the [Files] section, and FileCopy() them multiple times depending on the user-defined settings and properties of the files.

Is it possible to access the sources in the [Files] section using a script?

like image 354
Martijn Avatar asked Sep 26 '22 03:09

Martijn


1 Answers

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

like image 194
Martin Prikryl Avatar answered Oct 18 '22 00:10

Martin Prikryl