Search code examples
inno-setup

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


I have Inno Setup 6.1.2 setup script where the version main.sub.batch is formed like this:

#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])

Later in the setup part, I use it for the name of setup package:

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

The resulting filename will be app.1.0.2.x64.exe, which is mighty fine. To make it perfect, I'd like to end up with form app.1.00.002.x64.exe with zero-padded components.

I did not find anything like PadLeft in documentation. Unfortunately I also fail to understand how to use my own Pascal function in this context. Can I define a function in Code section for this?


Solution

  • A quick and dirty solution to pad the numbers in the 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])
    

    If you want a generic function for the padding, use this:

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

    Use it like:

    #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 Script Code (like this one) won't help here, as it runs on the install-time, while you need this on the compile-time. So the preprocessor is the only way.