inno-setup - Inno Setup - 在安装程序中获取文件信息

标签 inno-setup

如何获取有关安装程序中包含的文件的信息(最后修改时间戳、文件大小)?通过使用文件的路径,很容易引用磁盘上的文件。但是当文件没有路径时,如何在安装程序中引用它呢?

当安装程序初始化时,我想检查磁盘上是否已经有任何要安装的文件。对于那些已经在磁盘上的文件(相同的上次修改时间戳和相同的文件大小),我想为用户显示一个窗口,以便能够选择他们想要覆盖的文件。

最佳答案

以下preprocessor代码将生成一个 Pascal 脚本函数,该函数返回给定相对文件路径的时间戳字符串:

#define SourcePath "C:\myappfiles"

function GetSourceFileDateTimeString(FileName: string): string;
begin

#define FileEntry(FileName, SourcePath) \
    Local[0] = GetFileDateTimeString(SourcePath, "yyyy/mm/dd hh:nn:ss", "-", ":"),\
    "  if SameText(FileName, '" + FileName + "') then " + \
        "Result := '" + Local[0] + "'" + NewLine + \
    "    else" + NewLine

#define ProcessFile(Root, Path, FindResult, FindHandle) \
    FindResult \
        ? \
            Local[0] = FindGetFileName(FindHandle), \
            Local[1] = (Len(Path) > 0 ? Path + "\" : "") + Local[0], \
            Local[2] = Root + "\" + Local[1], \
            (Local[0] != "." && Local[0] != ".." \
                ? (DirExists(Local[2]) \
                      ? ProcessFolder(Root, Local[1]) \
                      : FileEntry(Local[1], Local[2])) \
                : "") + \
            ProcessFile(Root, Path, FindNext(FindHandle), FindHandle) \
        : \
            ""
#define ProcessFolder(Root, Path) \
    Local[0] = FindFirst(Root + "\" + Path + "\*", faAnyFile), \
    ProcessFile(Root, Path, Local[0], Local[0])
    
#emit ProcessFolder(SourcePath, "")
  RaiseException(Format('Unexpected file "%s"', [FileName]));
end;

生成的脚本如下:

function GetSourceFileDateTimeString(FileName: string): string;
begin
  if SameText(FileName, 'sub1\file1.exe') then Result := '2022-02-16 18:18:11'
    else
  if SameText(FileName, 'sub2\file2.exe') then Result := '2022-02-16 18:18:11'
    else
  if SameText(FileName, 'file3.exe') then Result := '2022-02-19 09:50:14'
    else
  if SameText(FileName, 'file4.exe') then Result := '2022-02-19 09:50:14'
    else
  RaiseException(Format('Unexpected file "%s"', [FileName]));
end;

(参见 Inno Setup: How do I see the output (translation) of the Inno Setup Preprocessor?)

关于inno-setup - Inno Setup - 在安装程序中获取文件信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71140338/

相关文章:

inno-setup - 将 Next 按钮更改为 Install with DisableReadyPage 指令的官方 Inno Setup 代码不起作用

inno-setup - 如何向 Inno Setup OuterNotebook 添加新页面?

checkbox - 将 Inno Setup 子组件显示为同级组件,并在复选框中显示检查而不是方形

xml - 如何将应用程序路径位置保存到 XML 节点中

python - innosetup 创建的可执行文件中出现 "Failed to execute script"错误

inno-setup - 检查先前是否在 Inno Setup 中下载过组件

inno-setup - 如何通过更改 inno 安装脚本在空白的欢迎页面中添加新文本?

installation - 如何使用 Inno Setup 安装程序升级 MSI 安装?

inno-setup - 使用 Inno Setup 静默运行安装,无需任何下一步按钮或安装按钮

inno-setup - 使用 InnoSetup 递归设置文件夹权限