Using VBScript to copy files to a folder in a MSI Setup Project
Last week I found out that one of our admin apps on the server was not installed currently. It is an app that is installed in ArcMap, and the reason it didn’t work is that ArcMap has been installed on d:\programs files
instead of c:\program files
.
The msi depended on the location in c:\
.
I opened the solution to try to fix the installer. I found that when you install ArcDesktop it adds an environment variable named ARCGISHOME
. I also found it is impossible to take a dll from a primary output in an msi and order the msi to put it in another folder.
So I used a custom action in the install phase with a VBScript file.
In order to do that you need to:
- File –> New –> Text File
- Save the text file with .vbs suffix under the setup project folder in the file system
- Right click on the setup project is Visual Studio –> View –> Custom Actions
- Right click on Install –> Add Custom Action
- Select the vbs file and click OK.
Open the vbs file and write the following script:
Set wsShell = WScript.CreateObject("WScript.Shell")
Dim targetDir = Session.Property("CustomActionData")
RunScript = "xcopy """ + targetDir + "\somefiles"" ""%ARCGISHOME%bin"""
wshell.run RunScript
In the vbs file properties under Custom Action Data
enter [TARGETDIR]
. This tells the msi to set the CustomActionData
property as the folder in which the msi deploys it’s main files.
Now, try to run this and surprise surprise... It doesn't work...
After of couple of frustrating debug trials, I found that when running VBScript in a MSI host, it can not used environment variables.
In order to use environment variables we can use the WScript method called ExpandEnvironmentStrings:
Set wsShell = WScript.CreateObject("WScript.Shell")
Dim targetDir = Session.Property("CustomActionData")
arcGisHomeDir = wsShell.ExpandEnvironmentStrings("%ARCGISHOME%")
RunScript = "xcopy """ + targetDir + "\somefiles"" """ + arcGisHomeDir + "bin"""
wshell.run RunScript
This one works perfectly.
Use it wisely.