yanivRa
Posts: 3
Joined: Thu Mar 30, 2006 12:45 pm

Defining a folder name based on property value

Hello
Can i define a folder name , something like "MyFolder version[ver]"
and define the ver as a property

Thanks in advance
Yaniv
Ionut
Posts: 605
Joined: Tue Nov 22, 2005 11:29 am
Contact: Website

Hi,
Can i define a folder name , something like "MyFolder version[ver]"
and define the ver as a property
Yes, you can, but you need to use a Custom Action as follows:

1. In the Files and Folders page, add all the required files/folders for your package.

2. You need to find out what is the ID of the folder for which you want to change the name at install time. In order to do this:
a) Switch to the Registry page and select the "New Value" toolbar button.
b) Click the "Folder" button (in the "Edit Registry Entry" dialog) and then choose the appropriate directory in the tree control.
c) The directory ID will be inserted in the "Data" field and will look like: [folder_DIR].
d) Write down this ID (without the square brackets) - you will need it to set up the Custom Action.
e) Press "Cancel" to close the "Edit Registry Entry" dialog.

3. Switch to the "Install Parameters" page and define your Property, for example:
- Name: MYVER
- Value: 1.3

Note that you can also specify the value of the Property at install time using the "msiexec" command-line, as follows:

Code: Select all

msiexec /i package.msi MYVER="1.9"
Also note that you need to define a Public Property (all the letters are upper-case).

4. Create a new text file ("AlterPath.vbs") and copy the following code into it:

Code: Select all

AlterTargetPath("folder_DIR"), " version", "MYVER"

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function that will alter the TargetPath for a specified folder
'
' @param strFolderId The ID of the folder as it appears in the 
'        Directory table (from the MSI database)
' @param strAppend String that will be appended to the folder name
' @param strVerProp The name of the Property whose value will be 
'        appended to the folder name after strAppend
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function AlterTargetPath(strFolderId, strAppend ,strVerProp)
  Dim strTarget, strTemp
  
  ' Get the TargetPath for the given folder 
  strTarget = Session.TargetPath(strFolderId)
  
  ' Remove the trailing "\" if necessary
  If Right(strTarget,1) = "\" Then
    strTemp = Left(strTarget,Len(strTarget)-1)
  End If
  
  ' Set the TargetPath by appending strAppend 
  ' and the value of the Property strVerProp
  Session.TargetPath(strFolderId) = _
        strTemp   & strAppend & Session.Property(strVerProp)
  
  ' Useful for debugging, comment it when releasing the package
  MsgBox Session.TargetPath(strFolderId)
End Function
Modify the call to "AlterTargetPath" as follows:
- the first parameter is the folder ID you have determined in step 2 above.
- the second argument is a string that will be appended to the folder name (before the Property value)
- the third parameter is the Property you have defined in step 3 above and whose value will also be appended to the folder name, after the string specified by the second argument

5. Finally, switch to the Custom Actions page, and make sure that "CostFinalize" is visible under "InstallExecuteSequence" ("Show Standard Action -> Before Initialization -> CostFinalize"). Add a "New Attached Custom Action" on the "CostFinalize" stage and select the script file you have created previously. Set the following properties for this Custom Action:
- Function Name: (Must be blank)
- Execution Properties: "Synchronous execution, check return code"
- Execution Condition: (Not Installed)


This script is provided merely as an example and you should use a DLL Custom Action for efficiency. Make sure that you understand it entirely before using it, because it alters folder paths during installation (which might cause installation/uninstallation problems if the script is not used properly). Although I have tested this script for several possible scenarios, use it at your own risk.

Hope this helps.

Regards,
Ionut
Denis Toma
Advanced Installer Team
http://www.advancedinstaller.com/

Return to “Common Problems”