Skip to content

Replace variables in file

This action lets you replace all variable holders in the contents of an existing text file. This file is then saved to another file.

This custom action is useful if you want to use templates for configuration files like INI, REG, TXT, XML... and store values of variables, to set initial options for your program or store information about the package's execution, such as the user's name or the destination directory. See example below.

Property Name Data Type Description
DestFile String Path to the destination text file that the package will create (the filename does not necessarily require the .txt extension). Variables are possible. If the file exists, it will be overwritten.
SourceFile String Path to the existing source text file that the package should use as a template (the filename does not necessarily require the .txt extension). Variables are possible.

Notes

  • If the destination text file exists, it is overwritten.

  • The uninstaller will not remove this file unless you specify it.

  • All existing variables and resource strings are replaced.

Example

You want to store the destination path in an INI file for your application.

Here is an INI sample:

[Parameters]
Destination=%DESTPATH%
IsWin64=%IS64OS%

We store this content in a template file named template.ini. Then we add it to the package's overhead thanks to the Loader Temporary Resources in Advanced Build Options. This will ensure the TEMPLATE.INI file will be available when the package runs, in the temporary folder. Since this is a template file only, it is only required temporarily. The package will erase it when it closes.

Then we add our custom action "Replace variables in file" to the "After File Extraction" event, with these properties:

SourceFile=%PBSFXPATH%\Template.ini
DestFile=%DESTPATH%\Settings.ini

%DESTPATH%\Settings.ini tells the package to store the new INI file with variables correctly replaced in the destination folder of our application, with the filename "settings.ini", so that our application can find it.

All available actions