Search and Replace Script with AutoIt

The Problem

Hardcoded Paths, Licenses and Computer Names are often saved in static files. MSI doesn’t have a standard action for changing content in a static file. Because of that I have created an AutoIt script that can handle this lack of build in Windows Installer functionality.

AutoIt is a very common tool for system administrators and software packagers as the compiled binary file does not depend on a runtime or prerequisite

The AutoIt Script

This is the code of the script:

$strFilePath = $CmdLine[1]
$strFind = $CmdLine[2]
$strReplace = $CmdLine[3]$strText = FileRead($strFilePath,FileGetSize($strFilePath))
$strText = StringReplace($strText, $strFind, $strReplace)
FileDelete($strFilePath)
FileWrite($strFilePath,$strText)

The Command Line – as example replacing a server name in a text file

Replace.exe “[PathToTheFile]” “[SearchForString]” “[ReplaceWithString]”

PathToTheFile: exp. “C:\Program Files (x86)\Mathcad\Mathcad Prime 1.0\MathcadPrime.exe.config”

SearchForString: exp. “7788@test”

ReplaceWithString: exp. “7788@licsvr”

Implementation in a Windows Installer based Package

Add a new Custom Action in the Deferred Context:

 

Enter the Details for the Custom Action:

Select the script via clicking onto “Browse”.

Command Line: “[#MathcadPrime.exe.config]” “7788@test” “[ONTXPRIMELICENSEPORT]@[ONTXPRIMELICENSESERVER]”

 

Enter the Properties for the Custom Action:

Don’t forget to set “Processing” to “Synchronous, Ignore exit code”.

Post Navigation