Skip to main content

1E SDK

FileContent.FindAndReplace

Method

FindAndReplace

Module

FileContent

Library

FileContent

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.

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.

@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

Notes

When 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.