Skip to main content

1E 8.1 (on-premises)

FileContent.AppendText

Method

AppendText

Module

FileContent

Library

FileContent

Action

Append text to a file.

Parameters

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

Text (string): The text string to be appended to an existing file or written to a new file.

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

NewLine (bool; optional, default false) If true an O/S-specific line separator sequence will be written before the Text is written or appended.

Encoding (string, optional, default "UTF8NOBOM"): File content encoding. Valid values are:

  • “ANSI”

  • “UTF8” - UTF8 with a byte order mark.

  • “UTF8NOBOM” - UTF8 without a byte order mark.

  • “UTF16LE” - UTF16 little endian byte order.

  • “UTF16BE” - UTF16 big endian byte order.

If the file already exists and has content then an encoding should not be specified.

Return values

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

Success (bool): Whether the method succeeded.

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

Example

Given an existing file accounts.txt, when the following code is executed:

@lines =
        SELECT " { \"ac\": \"62781973\", \"sc\": \"05-21-07\", \"dob\": \"17-10-1993\" }" AS line
  UNION SELECT ",{ \"ac\": \"01113201\", \"sc\": \"28-33-31\", \"dob\": \"12-01-1947\" }" AS line
  UNION SELECT ",{ \"ac\": \"87290123\", \"sc\": \"20-35-27\", \"dob\": \"09-12-1996\" }" AS line
  UNION SELECT ",{ \"ac\": \"72098345\", \"sc\": \"21-09-37\", \"dob\": \"07-03-1993\" }" AS line;

FileContent.WriteText(FilePath : "C:\\TIMS\\Data\\accounts.txt", Text : "{ \"closed\" : [", NewLine : true, Backup: true);

FOREACH @line IN @lines DO
    FileContent.AppendText(FilePath : "C:\\TIMS\\Data\\accounts.txt", Text : @line, NewLine : true);
DONE;

FileContent.AppendText(FilePath : "C:\\TIMS\\Data\\accounts.txt", Text : "]}", NewLine : true); 

A backup accounts.txt.bak file is created containing the original file contents, and accounts.txt will now contain:

{ "closed" : [
 { "ac": "62781973", "sc": "05-21-07", "dob": "17-10-1993" } 
,{ "ac": "01113201", "sc": "28-33-31", "dob": "12-01-1947" } 
,{ "ac": "72098345", "sc": "21-09-37", "dob": "07-03-1993" } 
,{ "ac": "87290123", "sc": "20-35-27", "dob": "09-12-1996" } 
]}

Platforms

  • Windows

  • Linux

  • MacOS

Notes