Read File Lines Functions

The following functions allow reading the lines of a file in the predefined folder. The file system location allowed to read files 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 read operation is completed.

fileReadLinesSync

The fileReadLinesSync function allows reading a specific <count> of lines, starting from <startLine>, of the file specified by <relativePath> in the predefined folder, with the given <encoding>.

fileReadLines

The fileReadLines function allows reading a specific <count> of lines, starting from <startLine>, of the file specified by <relativePath> in the predefined folder, with the given <encoding>.

Then it calls the <callback> with the operation result.

Syntax

 

(Synchronous read lines)

var result = fileReadLineSync(<relativePath>, <startLine>, <count>, <encoding>)

 

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

fileReadLine(<relativePath>, <callback>, <startLine>, <count>, <encoding>)

Parameters Usage

Parameter

Type

Default

Use

Annotation

relativePath

String

-

Mandatory

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

callback

Function

-

Mandatory for asynchronous calls

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

startLine

Integer

1

Optional

Index of the line to start reading from. If equal to 1, read from the first line of the file.

count

Integer

0

Optional

Number of lines to read, starting from <StartLine>. If equal to 0, read until the end of file.

encoding

String

"utf8"

Optional

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

The callback function is declared as follows:

 

function fileReadLinesCallback(<fileReadLinesResult>)

{

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

}

 

Where <fileReadLinesResult> is the result object.

Result

The functions fileReadLinesSync and fileReadLinesCallback return the FileReadResult object.

Error Handling

Errors can occur in case:

Note that path errors that occur when the fileReadLinesmethod is called produce a specific error message returned in result.error.

Examples of Use

 

How to read some lines of an ASCII file

Read 5 lines, starting from line 10, of an ASCII file in the shared scripting predefined folder. Then print to the Console expander any error occurred or the requested lines and their count.

var result = fileReadLinesSync("MyFile.log", 10, 5, "ascii")

if (result.error)

{

    console(result.error)

}

else

{

    console("Lines count: {0}", result.data.length)

    console("Lines content:{0}", result.data)

}

 

How to read asynchronously the lines of a file

Read the lines of a file in the shared scripting predefined folder, starting from line 20 to the end of file. Then print to the Console expander any error occurred or the requested lines and their count.

fileReadLines("MyFile", readLinesCallback, 20)

function readLinesCallback(readLinesResult)

{

    if (result.error)

    {

        console(result.error)

    }

    else

    {

        console("Lines count: {0}", result.data.length)

        console("Lines content:{0}", result.data)

    }

}

 

How to read all the lines of a file

fileReadLinesSync("Log.txt")