Skip to main content

1E 9.x (on-premises)

FileContent.FindAndAppend

Method

FindAndAppend

Module

FileContent

Library

FileContent

Action

Appends a string to the end of a file if it does not contain a specified 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.

AppendString (string): The string that will be appended to the end of the file. This can be the same as FindString.

Backup (bool; optional, default false): Whether or not to backup the file prior to any appending. 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.

Return values

Returns 'Success - No Content' (empty table) if the string is found (file not updated), or the file is not found.

If the file is found and updated the following are returned as a table:

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

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

LineContent (string): The content of the line after appending AppendString.

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

Example

Append the line 'AuthenticationPolicy=required' if it isn't present in example.conf, regardless of case, making a backup of the file before the append occurs.

@FilePath = "C:\\temp\\Examples\\Example.conf";
@findString = "AuthenticationPolicy=required";
@appendString = "AuthenticationPolicy=required";
FileContent.FindAndAppend(FilePath: @FilePath, FindString: @findString, AppendString: @appendString, Backup:true, CaseSensitive:false);

Platforms

  • Windows

  • Linux

  • MacOS

Notes

Text is appended only if the find string does not exist in the file; this method should have been called FindElseAppend.