简体   繁体   English

Inno Setup:为文件夹及其子文件夹中的所有文件动态添加组件

[英]Inno Setup: Dynamically add a component for all files in a folder and its subfolders

Inno Setup: Dynamically add all files in a folder and add a component tag, so during running of the setup the user can select custom setup and select which files to copy. Inno Setup:动态添加文件夹中的所有文件并添加组件标签,因此在安装程序运行期间,用户可以选择自定义安装程序并选择要复制的文件。

I'd like to create a Inno Setup file that will grab files in a folder that users can put in there, without having to modify the Inno Setup file each time a new file is added.我想创建一个 Inno Setup 文件,它将在用户可以放入的文件夹中抓取文件,而无需在每次添加新文件时修改 Inno Setup 文件。 At the same time, I need the user of the setup file to be able to select which files to copy.同时,我需要安装文件的用户能够选择要复制的文件。

If I do something like this:如果我这样做:

Source: "D:\SomeDirectory\*"; DestDir: "{app}"; \
   Flags: ignoreversion recursesubdirs createallsubdirs; Components: dlls

The custom setup only shows the option to copy or not to copy the entire folder.自定义设置仅显示复制或不复制整个文件夹的选项。

Assuming the files are available on compile time, you can use Inno Setup Preprocessor recursive macro to generate the [Files] and [Components] sections.假设文件在编译时可用,您可以使用Inno Setup Preprocessor递归宏来生成[Files][Components]部分。

This code is partially based on Generating Inno Setup file flags programmatically .此代码部分基于以编程方式生成 Inno 安装文件标志

#pragma parseroption -p-

#define FileEntry(Source, Dest) \
    Local[0] = Copy(Dest, 2, Len(Dest) - 1), \
    Local[1] = StringChange(Local[0], ".", ""), \
    "[Files]\n" + \
    "Source: " + Source + "; DestDir: {app}" + ExtractFileDir(Dest) + \
        "; Components: " + Local[1] + "\n" + \
    "[Components]\n" + \
    "Name: " + Local[1] + "; Description: " + ExtractFileName(Dest) + "\n"

#define DirEntry(Source, Dest) \
    Local[0] = Copy(Dest, 2, Len(Dest) - 1), \
    Local[1] = StringChange(Local[0], ".", ""), \
    "[Components]\n" + \
    "Name: " + Local[1] + "; Description: " + ExtractFileName(Dest) + "\n"
    
#define ProcessFile(Source, Dest, FindResult, FindHandle) \
    FindResult \
        ? \
            Local[0] = FindGetFileName(FindHandle), \
            Local[1] = Source + "\\" + Local[0], \
            Local[2] = Dest + "\\" + Local[0], \
            (Local[0] != "." && Local[0] != ".." \
                ? (DirExists(Local[1]) \
                     ? DirEntry(Local[1], Local[2]) + \
                           ProcessFolder(Local[1], Local[2]) \
                     : FileEntry(Local[1], Local[2])) \
                : "") + \
            ProcessFile(Source, Dest, FindNext(FindHandle), FindHandle) \
        : \
            ""

#define ProcessFolder(Source, Dest) \
    Local[0] = FindFirst(Source + "\\*", faAnyFile), \
    ProcessFile(Source, Dest, Local[0], Local[0])

#pragma parseroption -p+

#emit ProcessFolder("D:\SomeDirectory", "")

If D:\SomeDirectory contains these files:如果D:\SomeDirectory包含这些文件:

file1.txt
file2.txt
sub1\file11.txt
sub1\file12.txt
sub2\file21.txt
sub2\file22.txt

The above code will generate:上面的代码将生成:

[Files]
Source: D:\SomeDirectory\file1.txt; DestDir: {app}; Components: file1txt
[Components]
Name: file1txt; Description: file1.txt
[Files]
Source: D:\SomeDirectory\file2.txt; DestDir: {app}; Components: file2txt
[Components]
Name: file2txt; Description: file2.txt
[Components]
Name: sub1; Description: sub1
[Files]
Source: D:\SomeDirectory\sub1\file11.txt; DestDir: {app}\sub1; Components: sub1\file11txt
[Components]
Name: sub1\file11txt; Description: file11.txt
[Files]
Source: D:\SomeDirectory\sub1\file12.txt; DestDir: {app}\sub1; Components: sub1\file12txt
[Components]
Name: sub1\file12txt; Description: file12.txt
[Components]
Name: sub2; Description: sub2
[Files]
Source: D:\SomeDirectory\sub2\file21.txt; DestDir: {app}\sub2; Components: sub2\file21txt
[Components]
Name: sub2\file21txt; Description: file21.txt
[Files]
Source: D:\SomeDirectory\sub2\file22.txt; DestDir: {app}\sub2; Components: sub2\file22txt
[Components]
Name: sub2\file22txt; Description: file22.txt

In installer, you will get:在安装程序中,您将获得:

在此处输入图像描述


Though note that number of files you can process this way is limited by preprocessor stack.请注意,您可以通过这种方式处理的文件数量受预处理器堆栈的限制。

If you get hit by that, the other (though ugly and complex) way is to use user defined procedures.如果您遇到这种情况,另一种(虽然丑陋且复杂)的方法是使用用户定义的过程。 For an example of implementing a recursive files processing both using approach shown here and using user defined procedures, see Inno Setup - Recurse sub directories without creating those same sub directories .有关使用此处显示的方法和用户定义的过程实现递归文件处理的示例,请参阅Inno Setup - Recurse sub directories without creating those same sub directories

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何最好地删除文件夹及其子文件夹中的所有文件 - How best to delete all files in a folder and its subfolders 按类型和大小对文件夹及其子文件夹中的文件进行排序 - Sort files in a folder and its subfolders by type and size 如何在C ++上获取文件夹中的所有文件及其子文件夹的名称和字符串数组的路径? - How to get all files in a folder and its subfolders names&paths to string array on c++? 在文件夹和子文件夹中查找所有文件的另一种方法 - Alternative way to find all files in a folder and subfolders Gulp –如何将文件复制到文件夹的所有子文件夹中 - Gulp – How to copy files into all subfolders of a folder 创新设置:隐藏文件 - Inno setup: hide files Mac OS - 批量重命名文件夹中的所有文件但忽略所有子文件夹 - Mac OS - Batch Rename All Files in Folder but Disregard All SubFolders LibGDX获取文件夹+子文件夹中所有文件的文件句柄数组 - LibGDX get filehandle array for all files in folder + subfolders 如何扫描文件夹中的所有文件,包括VC ++ 2008中的子文件夹? - How to scan all files in a folder, including subfolders in VC++ 2008? 获取将返回列表的文件夹和子文件夹中所有文件的方法 - Method to get all files within folder and subfolders that will return a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM