Linking Subfolders to Features Using VBScript

InstallShield 2024

The VBScript example simulates dynamically linking to subfolders. By editing .ini files, you tell the script which folders and subfolders to link to, how to create the components for the files, and with which features to associate the new components.

The following are requirements for using the VBScript example:

You must have Windows Scripting Host to run a .vbs file.
InstallShield must be installed on the system before you can use the automation interface and .ini file reader.
The script expects two command-line arguments:
The fully qualified path to an existing setup project (.ism file).
The fully qualified path to an .ini file (Links.ini in this example) that describes each feature that you want a given folder’s files and subfolders added to. Furthermore, each feature can point to a different .ini file (Template.ini) that describes the properties of the components that will be created for the files in each folder and subfolder.

The source files for this example are found in the following directory:

InstallShield Program Files Folder\Samples\WindowsInstaller\Automation Interface\Add Files and Components

Each of the files is described below.

LinkToFeature.vbs

The script in the LinkToFeature.vbs file is designed to read the associated Links.ini file and add the files from the specified folders and subfolders to the specified features. It organizes them into components based on the model you construct in Template.ini. To start the script, pass it the path to your setup project and the path to Links.ini, respectively, at the command line:

LinkToFeature.vbs "C:\MySetups\Malaprop.ism" "C:\Automation Interface\Links.ini"

You can examine some of the relevant sections of LinkToFeature.vbs to see exactly how it works. After opening the .ism file specified at the command line, the script creates a collection of every feature in the project starting at line 53. This collection is later used to make sure that the features listed in Links.ini are present in the project.

' The scripting dictionary is defined in Scrrun.dll

Dim pFeatCol

Set pFeatCol = CreateObject("Scripting.Dictionary")

 

' Create a collection of all of the feature names in the project

For Each ISWIFeature In ISWIProject.ISWIFeatures

    pFeatCol.Add ISWIFeature, ISWIFeature.Name

    DoSubFeature ISWIFeature, pFeatCol

Next

Next, LinkToFeature.vbs opens Links.ini using the InstallShield IniReader. As the comment on line 70 explains, you can use this utility if it serves your needs or otherwise substitute your own. Once Links.ini is open, the rest of the script is controlled by the For loop beginning at line 80 and ending at line 202:

For Each pLinksSection In LinksIniFile.Sections

The purpose of this loop is to read each section in Links.ini. Each section contains the name of a feature that you want to add the new components to and the folder where the files and subfolders can be found. After getting each key’s value, there is a nested For loop, stretching from line 102 to 201, which loops through every feature in the collection pFeatCol and compares it to the name of the feature in Links.ini:

  For Each ISWIFeature In pFeatCol

        ' See if the feature in Links.ini matches one in the setup project.

        If (ISWIFeature.Name = linksFeature) Then

 

Since this loop is used for almost the remainder of the script, you can assume that everything that is done acts on the current feature. Next, it checks if the Action key in this Links.ini section is set to Delete and deletes all of the feature’s components if so:

            If (linksAction = "Delete") Then

                For Each ISWIComponent In ISWIFeature.ISWiComponents

                    ISWIProject.DeleteComponent ISWIComponent

                Next

            End If

 

At line 27, the script creates a collection of the specified folder and each subfolder:

            Set pFolders = CreateObject("Scripting.Dictionary")

            pFolders.Add folder, folder.Path

            GetSubFolders pFolders, folder

 

From line 136 to 163, the script opens the Template.ini file that contains the template for all of the components that will be added to the current feature:

            Set iniFile = CreateObject("IniReader.IniFile")

            iniFile.Load (linksTemplateFile)

 

LinkToFeature.vbs does its real work in the For loop that begins on line 169 and ends on 199:

            ' Create a component for the root folder and all of its subfolders.

            For Each folder In pFolders

Based on the description in Template.ini, it forms the component’s name, adds it to the project, associates it with the current feature, sets the component’s properties, and then adds the files in the current folder to the component.

Finally, the .vbs file saves and closes the setup project.

Links.ini

Each section of this .ini file contains the following keys, which LinkToFeature.vbs reads to determine which folders and subfolders to add to each feature in your project:

.ini File Keys in Sample File

Key

Description

[LinkN]

;Unique section name

Feature=FeatureName

;Name of existing feature in the project

Source=C:\Data Files\Executables

;Path to the root folder

TemplateFile=Template.ini

;Path to the component template .ini file

ComponentTemplate=Template1

;Section name in Template.ini for this feature's components

Action=Delete

;Delete tells LinkToFeature.vbs to permanently delete all of this feature's components before recreating them. Leave this value blank if you do not want the components destroyed.

Template.ini

Each section in Template.ini contains information for creating all of the components that will be associated with a feature. Each section in Links.ini must point to a specific component template file and section. The template file and sections are described below:

Template.ini File Sections

Section

Description

[TemplateName]

;Unique section name that identifies a particular template

Name=DefaultName

;This value is used to build the component's name in the format DLS_DefaultName_N

Destination=[ProgramFilesFolder]

;This is the root destination folder for the components. The names of the subfolders are then added to the root so that the files are installed to the correct relative folders.

Languages=1033

;List the decimal value of all of the component's languages, separated by a comma

Condition=VersionNT

;Author any condition you want to attach to each component

See Also