Contents

Method

FindAndReplace

ModuleFileContent
LibraryFileContent
Action

Replaces a string in a file with another string.

Parameters

FilePath (string): The full path of the file. Environment variables in the form %variable% for windows, and $variable for non-windows operating systems will be expanded.

The FilePath parameter name is only available from v3.2 onwards.

In v3.1 and earlier FileName must be used instead.  While the earlier name will still work, we recommend that, from v3.2 onwards, FilePath is used instead.

FindString (string): The string to search the file for.

ReplaceString (string): The string that will replace FindString

Backup (bool; optional, default false): Whether or not to backup the file prior to replacing. A copy of the file will be made in the same directory with the .bak suffix. If this already exists, an incrementing int will be added after the extension. If a backup cannot be created, this method will return an error.

CaseSensitive (bool, optional, default true): Whether or not to match case when searching for FindString in the file.

Occurrence (string; optional, default "All"): How the replacement occurs. Valid values are "First", "Last" and "All". "First" will only replace the first occurrence matched, "Last" will only replace the last, and "All" will replace all matches.

Return values

FilePath (string): The full path of the file, with any environment variables expanded.

LineNumber (int): The line number on which a replacement occurred. Lines are split on newline characters.

LineContent (string): The content of the line after a replacement occurred.

BackupFileName (string): The full path of the backup file created if Backup is true. A NULL is returned if Backup is false.

Example

Replace the first occurrence of 'AuthenticationPolicy=optional' with 'AuthenticationPolicy=required' regardless of case, making a backup of the file before the replacement occurs.

This uses the newer FilePath parameter name. Replace with FileName for v3.1 and older.
@FilePath = "C:\\temp\\Examples\\Example.conf";
@findString = "AuthenticationPolicy=optional";
@replaceString = "AuthenticationPolicy=required";
FileContent.FindAndReplace(FilePath: @FilePath, FindString: @findString, ReplaceString: @replaceString, Backup:true, CaseSensitive:false);
Platforms
  • Windows
  • Linux
  • MacOS
  • Solaris Intel
  • Solaris SPARC
NotesWhen a huge file is used with a lot of replacement, the Agent will consume high CPU and will take more time to process the instruction.