Access file list via script in InnoSetup

那年仲夏 提交于 2019-11-27 05:30:58

The idea here is to store the file names into the separate text file (the Source.txt here) where each line will be one file. The preprocessor will then generate the script for you. Actually it creates the array which contains the list of the files from the Source.txt and add all of its elements into the [Files] section and in the [Code] section it will fill the string list (here is used a list box to show the content).

Important:

You must have an extra non-empty line at the end of the Source.txt file, so just add e.g. ; at the end of the file.

Script:

#define FilesSource "d:\Source.txt"
#define FileLine
#define FileIndex
#define FileCount
#define FileHandle
#dim FileList[65536]
#sub ProcessFileLine
  #if FileLine != ""
    #expr FileList[FileCount] = FileLine
    #expr FileCount = ++FileCount
  #endif  
#endsub
#for {FileHandle = FileOpen(FilesSource); \
  FileHandle && !FileEof(FileHandle); \
  FileLine = FileRead(FileHandle)} \
  ProcessFileLine
#if FileHandle
  #expr FileClose(FileHandle)
#endif
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[Files]
#sub AddFileItem
  #emit 'Source: "' + FileList[FileIndex] + '"; DestDir: "{app}"'
#endsub
#for {FileIndex = 0; FileIndex < FileCount; FileIndex++} AddFileItem

[Code]
procedure InitializeWizard;
var  
  FileList: TStringList;
  FileListBox: TListBox;
  CustomPage: TWizardPage;
begin
  CustomPage := CreateCustomPage(wpWelcome, 'Theme selection page', '');

  FileListBox := TListBox.Create(WizardForm);
  FileListBox.Parent := CustomPage.Surface;
  FileListBox.Align := alClient;

  FileList := TStringList.Create;
  try
    #sub AddFileItemCode
      #emit '    FileList.Add(''' + FileList[FileIndex] + ''');'
    #endsub
    #for {FileIndex = 0; FileIndex < FileCount; FileIndex++} AddFileItemCode
    FileListBox.Items.Assign(FileList);
  finally
    FileList.Free;
  end;  
end;

#expr SaveToFile("d:\PreprocessedScript.iss")

Testing Source.txt:

MyProg.exe
MyProg.chm
Readme.txt
;

Output of testing PreprocessedScript.iss:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

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

[Code]
procedure InitializeWizard;
var  
  FileList: TStringList;
  FileListBox: TListBox;
  CustomPage: TWizardPage;
begin
  CustomPage := CreateCustomPage(wpWelcome, 'Theme selection page', '');

  FileListBox := TListBox.Create(WizardForm);
  FileListBox.Parent := CustomPage.Surface;
  FileListBox.Align := alClient;

  FileList := TStringList.Create;
  try
    FileList.Add('MyProg.exe');
    FileList.Add('MyProg.chm');
    FileList.Add('Readme.txt');
    FileListBox.Items.Assign(FileList);
  finally
    FileList.Free;
  end;  
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!