如何在 Inno Setup 中为 OutputBaseFilename 填充零版本组件

How to pad version components with zeroes for OutputBaseFilename in Inno Setup

我有 Inno Setup 6.1.2 安装脚本,其中版本 main.sub.batch 的构成如下:

#define AppVerText() \
  GetVersionComponents('..\app\bin\Release\app.exe', \
    Local[0], Local[1], Local[2], Local[3]), \
    Str(Local[0]) + "." + Str(Local[1]) + "." + Str(Local[2])

稍后在安装部分,我用它作为安装包的名称:

[Setup]
OutputBaseFilename=app.{#AppVerText}.x64

生成的文件名将是 app.1.0.2.x64.exe,这非常好。为了使其完美,我想以带有零填充组件的 app.1.00.002.x64.exe 形式结束。

我在文档中没有找到类似 PadLeft 的内容。不幸的是,我也不明白如何在这种情况下使用我自己的 Pascal 函数。我可以在 Code 部分为此定义一个函数吗?

填充 Inno Setup preprocessor:

中的数字的快速而肮脏的解决方案
#define AppVerText() \
  GetVersionComponents('..\app\bin\Release\app.exe', \
    Local[0], Local[1], Local[2], Local[3]), \
    Str(Local[0]) + "." + \
      (Local[1] < 10 ? "0" : "") + Str(Local[1]) + "." + \
      (Local[2] < 100 ? "0" : "") + (Local[2] < 10 ? "0" : "") + Str(Local[2])

如果您想要填充的通用函数,请使用:

#define PadStr(S, C, L) Len(S) < L ? C + PadStr(S, C, L - 1) : S

像这样使用它:

#define AppVerText() \
  GetVersionComponents('MyProg.exe', \
    Local[0], Local[1], Local[2], Local[3]), \
    Str(Local[0]) + "." + PadStr(Str(Local[1]), "0", 2) + "." + \
      PadStr(Str(Local[1]), "0", 3)

Pascal 脚本 Code(如 )在这里无济于事,因为它在安装时运行,而您在编译时需要它。所以预处理器是唯一的方法。