通过InnoSetup中的脚本访问文件列表

时间:2012-01-19 10:03:54

标签: inno-setup pascalscript

运行安装程序时,有没有办法从PascalScript访问文件列表([Files]部分中的条目)?我们试图直接从设置中运行应用程序,而不是必须安装它,这样可以更容易地维护文件列表。

1 个答案:

答案 0 :(得分:5)

这里的想法是将文件名存储到单独的文本文件(此处为Source.txt)中,其中每行将是一个文件。然后预处理器将为您生成脚本。实际上它创建的数组包含Source.txt中的文件列表,并将其所有元素添加到[Files]部分,并在[Code]部分填充字符串列表(此处)使用列表框来显示内容。)

重要:

Source.txt文件的末尾必须有一个额外的非空行,所以只需添加例如文件末尾的;

脚本:

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

测试Source.txt:

MyProg.exe
MyProg.chm
Readme.txt
;

测试输出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;