After quite a few times of having to update my software manually(and really one time is too many), I decided to embark on the difficult road of creating an updater where one had not existed. I turned to my trusted NSIS, which had worked well for me in the past(your mileage may vary).

My only complaint would be that not that many people use it(I know not a lot of people make windows installers anymore, but I needed to for this project.), which leads to confusing documentation, Q&A and resources kind of scattered around online. I had some difficulty finding out elementary basic examples of how to do the following with NSIS.

  • Prompt the user to choose the installation directory
  • Backup the existing directory with a datetime stamp
  • Mark the backup as readonly

These are pretty standard, and I assume pretty common requirements for an install or update. Below is my solution which is performs these simple tasks.

!include FileFunc.nsh
!insertmacro GetTime

SetCompressor /SOLID /FINAL lzma

# name the installer
OutFile "Installer.exe"

InstallDir $%HOMEDRIVE%

Page directory
Page instfiles

Section Backup
    Var /GLOBAL backupFolder
    Var /GLOBAL command

    # define the output path for this file
    setOutPath $INSTDIR

    DetailPrint "The user has chose the directory: $INSTDIR for the install."

    ${GetTime} "" "L" $0 $1 $2 $3 $4 $5 $6

    StrCpy $backupFolder "$INSTDIR.backup.$1-$0-$2.$4_$5_$6"

    StrCpy $command 'xcopy "$INSTDIR" "$backupFolder" /S /E /V /Y /I'
    DetailPrint $command
    ExecWait $command
SectionEnd 

Update!
The following code should be used with caution. I had to remove it, due to the permission implications of setting file/folder attributes. Many configurations will not allow user’s to set these attributes, especially on network drives. So you have been warned.

    ExecWait 'attrib +r "$backupFolder\*.*" /S /D'
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s