Function to add multiple source lines to Inno File

2019-05-26 17:13发布

Is there a function or preprocessor directive that can be used to add multiple lines to the Files section in Inno Setup? For example, I have numerous occurrences of a pattern similar to the following:

[Files]
Source: "{#SrcPath}\Dir1\FileName.*"; DestDir: {#DstPath}\Dir1;   
Source: "{#SrcPath}\Dir2\FileName.*"; DestDir: {#DstPath}\Dir2;  
Source: "{#SrcPath}\Dir3\FileName\*"; DestDir: {#DstPath}\Dir3\FileName; Flags: recursesubdirs  

And while I can just copy and paste the lines for each one, I was wondering if instead I could do something like this?

[Files]
AddFiles(FileName)

Unfortunately, I can't find any examples in the docs or online that illustrates how to do this. Is this possible?

标签: inno-setup
1条回答
Anthone
2楼-- · 2019-05-26 18:07

Define a preprocessor macro (template) using the #define directive like this:

#pragma parseroption -p-
#define FileTemplate(str FileName) \
  "Source: \"" + SrcPath + "\\Dir1\\" + FileName + ".*\"; DestDir: " + DstPath + "\\Dir1;\n" + \
  "Source: \"" + SrcPath + "\\Dir2\\" + FileName + ".*\"; DestDir: " + DstPath + "\\Dir2;\n" + \
  "Source: \"" + SrcPath + "\\Dir3\\" + FileName + ".*\"; DestDir: " + DstPath + "\\Dir3; Flags: recursesubdirs"
#pragma parseroption -p+

And expand the template using the #emit directive like this:

#define SrcPath "C:\srcpath"
#define DstPath "{app}"

[Files]
#emit FileTemplate("FileName1")
#emit FileTemplate("FileName2")

If you get the preprocessor to dump a preprocessed file, you will see that the code produces this:

[Files]
Source: "C:\srcpath\Dir1\FileName1.*"; DestDir: {app}\Dir1;
Source: "C:\srcpath\Dir2\FileName1.*"; DestDir: {app}\Dir2;
Source: "C:\srcpath\Dir3\FileName1.*"; DestDir: {app}\Dir3; Flags: recursesubdirs
Source: "C:\srcpath\Dir1\FileName2.*"; DestDir: {app}\Dir1;
Source: "C:\srcpath\Dir2\FileName2.*"; DestDir: {app}\Dir2;
Source: "C:\srcpath\Dir3\FileName2.*"; DestDir: {app}\Dir3; Flags: recursesubdirs

Implementation notes: I didn't find a way to emit a new-line in the default Pascal-style preprocessor strings, so I had to temporarily switch to C-style strings using the #pragma parseroption -p-.

I have posted a follow-up question:
Emit new line in Inno Setup proprocessor

查看更多
登录 后发表回答