通过InnoSetup中的脚本访问文件列表 [英] Access file list via script in InnoSetup
问题描述
运行安装程序时,是否可以通过PascalScript访问文件列表([文件]部分中的条目)?我们正在尝试使应用程序可以直接从安装程序运行,而不必安装它,这将使维护文件列表更加容易.
Is there any way to access the list of files (entries in the [Files] section) from PascalScript when running the setup? We're trying to make the application runnable directly from the setup, rather than having to install it, and this would make it easier to maintain the file list.
推荐答案
此处的想法是将文件名存储到单独的文本文件(此处为Source.txt
)中,其中每一行将是一个文件.然后,预处理器将为您生成脚本.实际上,它会创建一个包含Source.txt
中文件列表的数组,并将其所有元素添加到[Files]
部分中,在[Code]
部分中,它将填充字符串列表(此处使用列表框来显示内容.
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:
Source.txt
文件末尾必须有一个额外的非空行,因此只需添加例如. ;
在文件末尾.
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")
测试Source.txt:
Testing Source.txt:
MyProg.exe
MyProg.chm
Readme.txt
;
PretestedScript.iss测试的输出:
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;
这篇关于通过InnoSetup中的脚本访问文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!