Get the List of Files in a Directory Functions

The following functions allow getting the names of files in a directory 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.

The file names returned include their relative paths in the shared\scripting predefined folder.

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 operation is completed.

fileGetListSync

The fileGetListSync function allows getting the names of files that match the specified <searchPattern> in the directory specified by <relativePath> in the predefined folder. It can also search any subdirectories (<includeSubdirectories> = true).

fileGetList

The fileGetList function allows getting asynchronously the names of files that match the specified <searchPattern> in the directory specified by <relativePath> in the predefined folder. It can also search any subdirectories (<includeSubdirectories> = true).

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

Syntax

 

(Synchronous get files list)

var result = fileGetListSync(<relativePath>, <searchPattern>, <includeSubdirectories>)

 

(Asynchronous get files list. The result is provided in the callback method.)

fileGetList(<relativePath>, <callback>, <searchPattern>, <includeSubdirectories>)

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.

searchPattern

String

"*"

Optional

The search string to match against the names of files in path. This parameter can contain "*" (zero or multiple characters in that position) and "?" (zero or one character in that position). A search pattern equal to "*" or empty retrieves all the files. The search pattern is not case sensitive.

includeSubdirectories

Boolean

False

Optional

Whether to look into subdirectories.

The callback function is declared as follows:

 

function fileGetListCallback(<getListResult>)

{

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

}

 

Where <getListResult> is the result object.

The search pattern follows these rules:

Result

The functions fileGetListSync and fileGetListCallback return the FileGetListResult object.

Error Handling

Errors can occur in case:

Examples of Use

 

How to get the files in the shared scripting predefined folder and read their contents

var result = fileGetListSync(".")

if (result.files != null)

{

    for (var i = 0; i< result.files.length; i++)

        {

        var fileContents = fileReadSync(result.files[i])

        console("--- {0}\n{1}", result.files[i], fileContents.data)

    }

}

 

How to get and print (asynchronously) the list of files matching a given search pattern

Get and print (asynchronously) the list of files in directory "MyDir" that match the search pattern "Log*.txt"

fileGetList("MyDir", getListCallback, "Log*.txt")

function getListCallback(getListResult)

{

    if (getListResult.error)

        console(getListResult.error)

    else

        console(getListResult.files)

}

 

How to get the list of all the files in a directory ("MyDir") and its subdirectories

The following instructions are equivalent.

var result = fileGetListSync("MyDir", "", true)

 

var result = fileGetListSync("MyDir", "*", true)

 

var result = fileGetListSync("MyDir", null,

)