使用Inno Setup preprocessor 生成[Files] 部分的条目。
一种可能(并且相对较好和简单)的解决方案是使用递归宏,例如:
#pragma parseroption -p-
#define FileEntry(Source) \
"Source: " + Source + "; DestDir: {app}\n"
#define ProcessFile(Source, FindResult, FindHandle) \
FindResult \
? \
Local[0] = FindGetFileName(FindHandle), \
Local[1] = Source + "\\" + Local[0], \
(Local[0] != "." && Local[0] != ".." \
? (DirExists(Local[1]) ? \
ProcessFolder(Local[1]) : FileEntry(Local[1])) \
: "") + \
ProcessFile(Source, FindNext(FindHandle), FindHandle) \
: \
""
#define ProcessFolder(Source) \
Local[0] = FindFirst(Source + "\\*", faAnyFile), \
ProcessFile(Source, Local[0], Local[0])
#pragma parseroption -p+
#emit ProcessFolder("C:\kh25\dependencies")
尽管此解决方案有其局限性,并且可能会使预处理器因大量文件或深层目录结构而崩溃(适用于数千个文件)。
灵感来自answer by @Zlatko Karakaš 到Use Inno Setup PreProcessor to get the files and size of the source path and its subdirs。
更可靠(但丑陋和复杂)的解决方案是使用用户定义的程序。这很复杂,因为预处理器不支持用户定义过程的参数。
[Files]
#define FindHandle
#define FindResult
#dim InnerMask[65536]
#define InnerMask[0] ""
#sub ProcessFoundFile
#define InnerFileName FindGetFileName(FindHandle)
#define fileName InnerMask[InnerMaskWorkPosition] + InnerFileName
#if InnerFileName!="." && InnerFileName!=".."
#if DirExists(FileName)
#define Public InnerMask[InnerMaskPosition] FileName+"\"
#define Public InnerMaskPosition InnerMaskPosition + 1
#else
Source: {#FileName}; DestDir: {app}
#endif
#endif
#endsub
#sub ProcessInnerMaskPosition
#for { \
FindHandle = FindResult = \
FindFirst(InnerMask[InnerMaskWorkPosition]+"*", faAnyFile); \
FindResult; FindResult = FindNext(FindHandle)} ProcessFoundFile
#if FindHandle
#expr FindClose(FindHandle)
#endif
#endsub
#sub CollectFiles
#define Public InnerMaskPosition 1
#define Public InnerMaskWorkPosition 0
#for { \
InnerMaskWorkPosition = 0; InnerMaskWorkPosition < InnerMaskPosition; \
InnerMaskWorkPosition++} \
ProcessInnerMaskPosition
#undef Public InnerMaskPosition
#undef Public InnerMaskWorkPosition
#endsub
#expr InnerMask[0]="C:\kh25\dependencies\"
#expr CollectFiles
从answer by @René Martin到Use Inno Setup PreProcessor to get the files and size of the source path and its subdirs的递归扫描函数。
在您的 Inno Setup 脚本的最后添加一个 SaveToFile 调用,也可以查看预处理器生成的内容:
#expr SaveToFile(AddBackslash(SourcePath) + "Preprocessed.iss")
见Inno Setup: How do I see the output (translation) of the Inno Setup Preprocessor?
我对这个问题的回答解释了上述两种方法之间的区别:
Can Inno Setup Preprocessor be used to build a duplicated set of custom messages?