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 |
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:
- The specified
<relativePath>
: - Is missing, null, empty or white space
- Is not a string
- Is invalid (such as, path containing a root directory, wrong absolute path, path outside of the predefined folder, path or file name containing invalid characters, files or directories that have too long path). Note that invalid characters and the path length depend on the operating system.
- Does not exist. For example:
If the file does not exist, the error message is:'File
'[Installation Drive]:\[Installation Folder]\[Project Name]\shared\scripting\xyz.txt not found.'
If the folder does not exist, the error message is:
Data = '''Directory '[Installation Drive]:\[Installation Folder]\[Project Name]\shared\scripting\FolderAbc not found.'
Data = ''
- Does not represent a file
Note that path errors that occur when the fileReadLines
method is called produce a specific error message returned in result.error.
- The callback is missing, invalid, null, or empty (for asynchronous function only).
- The related error is logged only in the Trace Viewer.
<encoding>
is invalid.
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")