Files Exist Function

The following functions allow checking that files or directories exist in the predefined folder. The file system location allowed to access 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 check is completed.

fileExistsSync

The fileExistsSync function allows checking file or directories existence by <relativePath> in the predefined folder and synchronously returns True or False.

fileExists

The fileExists function allows checking asynchronously file or directories existence by <relativePath> in the predefined folder. Then it calls the <callback> with either True or False.

Syntax

 

(Synchronous check)

var result = fileExistsSync(<relativePath>)

 

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

fileExists(<relativePath>, <callback>)

Parameters Usage

Parameter

Type

Default

Use

Annotation

relativePath

String

-

Mandatory

Relative path of the file or directory 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.

The callback function is declared as follows:

function fileExistsCallback(<fileExists>)

 

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

}

 

Where <fileExists> is a boolean value indicating whether the file at <relativePath> exists in the predefined folder.

Result

The result is a boolean value indicating whether the file at <relativePath> exists in the predefined folder.

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.
  • The callback is missing, invalid, null, or empty (for asynchronous function only).
    • The related error is logged only in the Trace Viewer.

Examples of Use

 

How to check that a file exists and print to Console the result of the operation

The following instructions write "True" or "False" in the Console expander, depending on the existence of "MyFile.txt" in the folder "shared\scripting" of the current project.

var result = fileExistsSync("MyFile.txt")

console(result)

 

How to check asynchronously if a read-only file exists and print to Console the result of the operation

fileExists("MyReadOnlyFile.txt", myCallback)

 

function myCallback(result)

{

console(result)

}

 

When the callback is invoked, the Console expander contains "True" or "False", depending on the existence of "MyReadOnlyFile.txt" in the folder "shared\scripting" of the current project. This example generates the same output as a non-read-only file or directory. Also, the behavior is the same as the fileExistsSync method; it differs only because the result is returned in the callback.