Click here to Skip to main content
15,868,340 members
Articles / Programming Languages / VBScript
Article

NewSetupVersion for MSI Projects

Rate me:
Please Sign up or sign in to vote.
3.64/5 (8 votes)
19 Dec 2007CPOL2 min read 79.5K   1K   27   14
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)


Written By
Software Developer (Senior)
Germany Germany
Occupation: SW engineer since 1981.

Business: Control and automation systems.

History
The 80s: Assembler, PL/M, RMX OS.
The 90s: C, C++, pSOS, VxWorks, Structured Analysis, Windows, networking, project management
98-03: WinNT/2000, COM with C++/ATL, UIs with VB6, design with UML, IP protocols
The new millenium: WinXP, .NET (all versions), C#, WF, WCF, WPF, ...

Comments and Discussions

 
GeneralMy vote of 5 Pin
schnuff9-May-12 19:54
schnuff9-May-12 19:54 
GeneralMy vote of 5 Pin
oren.shnitzer11-Apr-12 8:56
oren.shnitzer11-Apr-12 8:56 
GeneralProject file not reloaded until after build Pin
amthog13-Jun-11 17:20
amthog13-Jun-11 17:20 
There is a problem if this script is run from the PreBuild step of the setup project you intend to update.

Changes to the project file are not reloaded until after the build completes.

This means the changes generated by the script will not apply to the current build but only to the next build.

Normally this may not be noticed unless you plan to display the version number in the installer or intend it to be visible in Add/Remove programs.

This can also be problematic where the sequence of version numbers is not "smooth" e.g. upgrading deployed versions that were built and installed before this script was in use, or where the version number is accidentally bumped backwards.
In the first case the first installer built with using the script will not upgrade deployed versions as it actually built using the old version and product code.
In the second case the new installer will still install but the next one built will fail.

There are a variety of (awkward, ugly) ways around this:
- build twice (yech!)
- macro to run script then build (requires developers to know about macro)
- Run script from Prebuild step of separate (empty) project with target setup project(s) listed as dependency.
- Customize the MSBuild
etc etc
GeneralVS2010 C# Pin
KristelFA6-Apr-11 1:12
KristelFA6-Apr-11 1:12 
GeneralRe: VS2010 C# Pin
Hans-Jürgen Schmidt6-Apr-11 7:16
Hans-Jürgen Schmidt6-Apr-11 7:16 
GeneralRe: VS2010 C# Pin
Harrrrr9-May-11 2:33
Harrrrr9-May-11 2:33 
GeneralRe: VS2010 C# Pin
KristelFA7-Jun-11 1:56
KristelFA7-Jun-11 1:56 
GeneralMy vote of 5 Pin
Valery Possoz10-Jan-11 3:28
professionalValery Possoz10-Jan-11 3:28 
GeneralIdeas... Pin
scH4MMER5-May-10 10:40
scH4MMER5-May-10 10:40 
GeneralIncrement the Version Number Pin
Pulikkan3-Feb-09 22:03
Pulikkan3-Feb-09 22:03 
AnswerRe: Increment the Version Number Pin
Hans-Jürgen Schmidt4-Feb-09 21:00
Hans-Jürgen Schmidt4-Feb-09 21:00 
GeneralRe: Increment the Version Number Pin
bbulla18-Aug-10 10:27
bbulla18-Aug-10 10:27 
GeneralRe: Increment the Version Number Pin
Member 121410532-Jun-16 1:33
Member 121410532-Jun-16 1:33 
GeneralUpdating using the build number Pin
Dave Hary19-Dec-07 7:17
Dave Hary19-Dec-07 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.