Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Method

FindAndAppend

ModuleFileContent
LibraryFileContent
Action


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

Note

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.

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=optional' if it isn't present in example.conf, regardless of case, making a backup of the file before the append occurs.

This uses the newer FilePath parameter name. Replace with FileName for v3.1 and older.
Code Block
@FilePath = "C:\\temp\\Examples\\Example.conf";
@findString = "AuthenticationPolicy=optional";
@appendString = "AuthenticationPolicy=optional";
FileContent.FindAndAppend(FilePath: @FilePath, FindString: @findString, AppendString: @appendString, Backup:true, CaseSensitive:false);


Platforms
  • Windows
  • Linux
  • MacOS
  • Solaris Intel
  • Solaris SPARC
Notes Text is appended only if the find string does not exist in the file. Perhaps this method should have been called FindElseAppend.