Count how many times your installer package has been executed

From NSIS Wiki
Jump to navigationJump to search
Author: Afrow UK (talk, contrib)


Description

This counts how many times the user has ran the installer package, and allows you to print the number anywhere you want. This script works under original NSIS and Modern UI, but this example is written under Modern UI. Very basic but effective :) It will allow you to put a 'You have ran this setup program x times so far!' message, or simply a 'x' message anywhere in your installer (where x is the count number) It will store info into an ini file in the Windows temp dir. You can write into the registry instead if you want.

How To Use

Define the Welcome page text...

    !define MUI_TEXT_WELCOME_INFO_TEXT "$R0\nIt is recommended that you close all other applications before resuming this program.\n\nClick Next to continue.$\n"

Note that the $R0 bit will put up the message. $R0 is defined further down in the script...

I found using $\n on MUI_TEXT_WELCOME_INFO_TEXT will hide anything after it, so use just \n instead. Gets info on next startup from count.ini. count.ini will be written on .onInstSuccess (after setup has ran for the 1st time)

Function .onInit ;start function .onInit
    ReadINIStr $2 "$TEMP\count.ini" "UserCount" "Value"

The 1st time the user runs the program, there will be no count.ini file in their $TEMP dir. This is because the count.ini file is created after the program finishes installing - it won't have got to that point yet!

IfFileExists "$TEMP\count.ini" "+3" ""
  StrCpy $1 "0"
goto +3
  IntOp $1 $2 + 1
  StrCpy $R0 "You have ran this setup program $2 times so far!\n\n"
 
FunctionEnd ;Finish function .onInit

Now, this last bit is called when the user has installed all files.

It will either be called automatically...

( AutoCloseWindow "true" )

...or will be called when user clicks on the exit/finish button...

( AutoCloseWindow "false" )

Function .onInstSuccess
    WriteINIStr "$TEMP\count.ini" "UserCount" "Value" "$1"
FunctionEnd

So, to get the user count, you would use $2

You can use $2 across your setup program, e.g.

    !define MUI_TEXT_INSTALLING_TITLE "Install Number $2 Status"

...will put that as the title of the install window page.

Using $R0 will produce the 'You have ran this setup program $2 times so far!' message. The first time you run the setup program, $R0 will print NO text.

-Stuart