Append Data to Files Functions

The following functions allow appending data to specific files in a predefined folder of the current project directory. The file system location allowed to append data is the folder shared\scripting of the current project. For example, [Installation Drive]:\[Installation Folder]\[Project Name]\shared\scripting.

Such functions can be run synchronously or asynchronously, and in the last case, it is necessary to provide a callback method that will be executed when the append operation is completed.

fileAppendSync

The fileAppendSync function behaves as the fileWriteSync function. The only difference is that, when a file already exists, the given <data> is appended to the end of file (instead of replacing the file content like with the fileWriteSync call).

fileAppend

The fileAppend function behaves as the fileWrite function. The only difference is that, when a file already exists, the given <data> is appended to the end of file (instead of replacing the file content like with the fileWrite call).

Syntax

 

(Synchronous append)

var result = fileAppendSync(<relativePath>, <data>, <encoding>)

 

(Asynchronous append. The result is provided in the callback method.)

fileAppend(<relativePath>, <data>, <callback>, <encoding>)

Parameters Usage

Parameter

Type

Default

Use

Annotation

relativePath

String

-

Mandatory

Relative path of the file to write in the shared\scripting folder of the current project.

data

String

Object

-

Mandatory

Data to write.

It can be a string or any object to which it makes sense to apply the ToString method (for example, simple data as integer, real or boolean values, or objects as BrowserObject or PropertyValue, and so on).

In case of arrays, the elements are printed as comma-separated values enclosed in square brackets.

callback

Function

-

Mandatory for asynchronous calls

Object that identifies the callback function invoked to provide the result of the operation.

encoding

String

"utf8"

Optional

File encoding. Allowed values are "utf8", "ascii", "Unicode", "utf32", "utf7".

The callback function is declared as follows:

 

function fileAppendCallback(<fileAppendResult>)

{

    //... Do callback stuff here ...

}

 

Where <fileAppendResult> is the result object.

Result

The functions fileAppendSync and fileAppendCallback return the FileWriteResult object.

Error Handling

Errors can occur in case:

Examples of Use

The examples of use of fileAppendSync and fileAppendare quite similar to the ones of fileWriteSync and fileWrite. The only difference is that infileAppendSync and fileAppend the file content is not replaced at each call.

 

How to append simple data or arrays with ASCII encoding

To avoid replacing the file content at each call as happens in the first example of fileWriteSync, use the fileAppendSync function.

Note the new line between fileAppendSync calls.

var filename = "Data.txt"

var encoding = "ascii"

 

fileAppendSync(filename, "hello world", encoding)

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, -123, encoding)

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, 1.23, encoding)

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, false, encoding)

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, 'a', encoding)

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, new Date(), encoding)

fileAppendSync(filename, "\r\n", encoding)

 

fileAppendSync(filename, ["hello world 1", "hello world 2", "hello world 3"])

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, [123, 456, -123, -456])

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, [12.3, -4.56, -7.89])

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, [true, false, true])

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, ['a', 'b', 'c', 'd', 'e'])

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, [new Date(), new Date()])

fileAppendSync(filename, "\r\n", encoding)

fileAppendSync(filename, ["hello", 'b', 1, -2.2, false, new Date()])

 

Once the above instructions have been executed, the ASCII file "Data.txt" located in the shared\scripting folder of the current project contains the following:

 

hello world

-123

1.23

False

a

8/31/2016 7:03:43 AM

[hello world 1, hello world 2, hello world 3]

[123, 456, -123, -456]

[12.3, -4.56, -7.89]

[True, False, True]

[a, b, c, d, e]

[8/31/2016 9:08:05 AM, 8/31/2016 9:08:05 AM]

[hello, b, 1, -2.2, False, 8/31/2016 9:08:05 AM]