Search/Replace File Contents
  • 14 May 2021
  • 1 Minute to read
  • Dark
    Light
  • PDF

Search/Replace File Contents

  • Dark
    Light
  • PDF

Article Summary

This is generated from the built in components of BuildMaster 7.0.0, and may be different than what you have installed (especially if you have extensions); go to [User Icon] -> Documentation within your BuildMaster instance to see exactly what operations are available.

Search/Replace File Contents

Searches a text file for a specified string and replaces it.

Script usage:

Replace-Text(
	[Include: <@(text)>],
	[Exclude: <@(text)>],
	[Directory: <text>],
	SearchText: <text>,
	[ReplaceWith: <text>],
	[Regex: <true/false>]
);

This operation may be prefixed with Files::, although this is a built-in namespace and isn't really necessary.

Arguments:

NameFormatScript UsageUsage Notes
Include
@(text)
Include
See KB#1119 to learn more about masking syntax.
Exclude
@(text)
Exclude
See KB#1119 to learn more about masking syntax.
Directory
text
Directory
Search text
text
SearchText
This argument is required.
Replace with
text
ReplaceWith
The text that replaces all occurrences of the search string found in matching files. If the Regex property is true, this property can replace matched group numbers as well, e.g.
Example line: Version: 0.0.0-b05f2ad
SearchText: Version: (\d+\.\d+\.\d+)(?<commitId>-\w+)?
ReplaceWith: Version: $ReleaseNumber`${commitId} (was `$1)
Example result: Version: 1.2.3-b05f2ad (was 0.0.0)

Note the backtick characters (`) used to escape the $ in the replacement text, which otherwise would be interpreted as OtterScript variables.
Use regex
true/false
Regex
Determines whether the search text should be interpreted as .NET Regular Expression syntax.

Example:

# Replaces the product version in an example vdproj file with the BuildMaster release number
Create-File example.vdproj
(
	Text: >>"Product"
		{
			"ProductVersion" = "8:1.0.0"
		}>>
);

Replace-Text
(
	Include: **.vdproj,
	SearchText: '"ProductVersion" = "(?<1>[0-9]+)\:[0-9]+\.[0-9]+\.[0-9]+"',
	ReplaceWith: '"ProductVersion" = "`$1:$ReleaseNumber"',
	Regex: true
);

Log-Information `$FileContents = $FileContents(example.vdproj);  # "ProductVersion" = "8:1.2.3"

Was this article helpful?

What's Next