Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VBScript

NewSetupVersion for MSI Projects

3.64/5 (8 votes)
19 Dec 2007CPOL2 min read 1   1K  
Auto-increment the version number of an MSI setup project.

Introduction

Visual Studio supports "Setup Projects", e.g., to create an MSI install package from a solution's output. To update an existing installation of such a package without first uninstalling the previous version requires some manual property changes in the setup project. This article presents a little script to do that automatically.

Regular Expressions are used to find the items to be modified and do the replacements. So, this article can also be considered as a little sample of Regular Expressions in VBScript.

Background

If an MSI package was previously installed on a PC and this package needs to be updated, then the installer complains that the software version is already installed and must be removed first.

The VS setup project has a property "RemovePreviousVersions", but setting this to True alone doesn't help. It is additionally required to change the version number and the product code. This is a manual procedure which can be done automatically in the PreBuildEvent using a little script.

Using the code

The script is written in VBScript and is quite simple. The task is to:

  • read the project file passed in the command line, e.g., Setup.vdproj
  • backup the original file, just in case something goes wrong ;-)
  • find the version number
  • increment the version number
  • replace the version number with the new value
  • replace the product code with a new GUID
  • replace the package code with a new GUID
  • write the updated project file back to disk

Regular Expressions are used to find the items to be modified and do the replacements. The Execute method finds the current version number entry. It can be extracted from the resulting Matches collection. The version number has three parts separated with a "." as the separator. For my purposes, I just increment the last part of the version number.

The RegExp object has a Replace method to find something and modify it.

It is also be interesting to see how new GUIDs can be created in VBScript:

VBScript
guid = CreateObject("Scriptlet.TypeLib").Guid
guid = left(guid, len(guid) - 2)

I haven't found out yet why the GUID has a trailing two bytes garbage, but it helped to just strip that off.

This is the whole script:

VBScript
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''  Increment the version number of an MSI setup project
''  and update relevant GUIDs
''  
''  Hans-Jürgen Schmidt / 19.12.2007  
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
set a = wscript.arguments
if a.count = 0 then wscript.quit 1

'read and backup project file
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(a(0))
s = f.ReadAll
f.Close
fbak = a(0) & ".bak"
if fso.fileexists(fbak) then fso.deletefile fbak
fso.movefile a(0), fbak

'find, increment and replace version number
set re = new regexp
re.global = true
re.pattern = "(""ProductVersion"" = ""8:)(\d+(\.\d+)+)"""
set m = re.execute(s)
v = m(0).submatches(1)
v1 = split(v, ".")
v1(ubound(v1)) = v1(ubound(v1)) + 1
vnew = join(v1, ".")
'msgbox v & " --> " & vnew
s = re.replace(s, "$1" & vnew & """")

'replace ProductCode
re.pattern = "(""ProductCode"" = ""8:)(\{.+\})"""
guid = CreateObject("Scriptlet.TypeLib").Guid
guid = left(guid, len(guid) - 2)
s = re.replace(s, "$1" & guid & """")

'replace PackageCode
re.pattern = "(""PackageCode"" = ""8:)(\{.+\})"""
guid = CreateObject("Scriptlet.TypeLib").Guid
guid = left(guid, len(guid) - 2)
s = re.replace(s, "$1" & guid & """")

'write project file
fnew = a(0)
set f = fso.CreateTextfile(fnew, true)
f.write(s)
f.close

Simple, isn't it ? (Well, OK, except for the Regular Expressions. I always have to look it up because I cannot remember the syntax...)

To use the script in the build process, just put it into any directory that is part of your search PATH. Add a PreBuildEvent to the setup project, e.g.:

NewSetupVersion.vbs "$(ProjectDir)Setup.vdproj"

Conclusion

The little VBScript shown above helped my MSI setup projects quite well. Maybe it can be of use for others too.

Oh, I forgot: I'm currently using VS2005. Maybe the keywords used here differ for other VS versions. But since it is "just script", it can be adapted to almost everything...

Happy updating!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)