Extracting a file path from a text file - Advanced Installer Community
Nick
Posts: 13
Joined: Tue Oct 17, 2017 1:46 am

Extracting a file path from a text file

Hello,

I've been getting to grips with Advanced Installer over the last few days and the design and overall intuitiveness of the software is impressive - thank you!

However, I'm finding the procedure to read data from a text file more of a challenge. Specifically, I want to check an existing *.cfg file in a known location for a custom installation path written by a executable command line switch argument. Unfortunately it seems a more conventional registry search to find this information won't be suitable for the project in question.

As far I can see, I need to:

1. Use a file search custom action to store the path of the *.cfg file in a property.
2. Use a second custom action to launch the file, execute some VBScript to read its contents, and store the relevant line in another property.

It's the second part I'm having trouble with, so any pointers or suggestion would be much appreciated.

To provide a bit more context, the *.cfg file has a structure something like this...

Title=Another Add-On
Path=C:\Users\Nick\Documents\Another folder

Title=My Add-On
Path=F:\My Add-On\My Folder
Title=A Different Add-On
Path=C:\Program Files (x86)\A Different Folder


...In which case I'd need to search for the string "My Add-On" and the parse the line below to extract the file path.

Many thanks,
Nick
Sorin
Posts: 663
Joined: Mon May 08, 2017 1:03 pm

Re: Extracting a file path from a text file

Hello and welcome to our Forums,

Thank you for the positive feedback!

You could implement this behavior using a VBS custom action that will get the path from the config file and store it in a property.

First, please take a look on how to get/set Windows Installer Properties in VBS custom actions : How to set an installer property using custom actions

We usually cannot provide support regarding custom actions code as our time resources are limited and we are dealing with a large number of support tickets, but I've written a script for your scenario :

Code: Select all

Dim Count
Dim strline
filename = Session.Property("RESULT_PROPERTY")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(filename, ForReading)

Const ForReading = 1
Count = 0

Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    Count=Count+1
    If instr(strLine, "My Add-On") <> 0 Then
	strLine = objFile.ReadLine
	strLine = Right(strLine, Len(strLine) - 5)
  
	
        Exit Do
    End If 
Loop
Session.Property("PATH") = strLine
objFile.Close


This script will open the text file whose path is stored in the RESULT_PROPERTY of your search and will search for "My Add-On" line and will store the next line in "strLine" variable. Then it will parse this string in order to remove the its first 5 characters ("Path="). The new value of "strLine" will be the actual path you are looking for. Then this path will be stored in PATH property.

You could add this code in a new "Execute inline script code" custom action configured to run after "Searches" in "Install Execution stage". This custom action should be conditioned with "RESULT_PROPERTY" condition - this will ensure it will only run when the search is successful (the config file is on disk).

Best regards,
Sorin
Sorin Stefan - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube
Nick
Posts: 13
Joined: Tue Oct 17, 2017 1:46 am

Re: Extracting a file path from a text file

Hello Sorin,

Many thanks for your help with this. For a scripting novice like myself this support is invaluable. I've tested your VBScript in a standalone editor and it works perfectly. (Once I'd figured-out with the help of this article that I needed to open the *.cfg file as unicode, using the "TriStateTrue" constant .)

I'll try not to bother you with further scripting questions, but I suspect I may have one-or-two further queries...

Thanks again,
Nick
Sorin
Posts: 663
Joined: Mon May 08, 2017 1:03 pm

Re: Extracting a file path from a text file

Hello Nick,

You are welcome!

Best regards,
Sorin
Sorin Stefan - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Building Installers”