Click here to Skip to main content
Click here to Skip to main content

NewSetupVersion for MSI Projects

, 19 Dec 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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 Wink | ;-)
  • 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:

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:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''  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)

Share

About the Author

Hans-Jürgen Schmidt
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 Pinmemberschnuff9-May-12 20:54 
GeneralMy vote of 5 Pinmemberoren.shnitzer11-Apr-12 9:56 
GeneralProject file not reloaded until after build Pinmemberamthog13-Jun-11 18:20 
GeneralVS2010 C# PinmemberKristelFA6-Apr-11 2:12 
GeneralRe: VS2010 C# PinmemberHans-Jürgen Schmidt6-Apr-11 8:16 
GeneralRe: VS2010 C# PinmemberMember 46193109-May-11 3:33 
GeneralRe: VS2010 C# PinmemberKristelFA7-Jun-11 2:56 
GeneralMy vote of 5 Pinmembervalery possoz10-Jan-11 4:28 
GeneralIdeas... PinmemberBrian Schallhammer5-May-10 11:40 
GeneralIncrement the Version Number PinmemberPulikkan3-Feb-09 23:03 
AnswerRe: Increment the Version Number PinmemberHans-Jürgen Schmidt4-Feb-09 22:00 
GeneralRe: Increment the Version Number Pinmemberbbulla18-Aug-10 11:27 
GeneralUpdating using the build number PinmemberDave Hary19-Dec-07 8:17 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 19 Dec 2007
Article Copyright 2007 by Hans-Jürgen Schmidt
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid