Delete Files or Directories Functions

The following functions allow deleting a file or a directory in a predefined folder. The file system location allowed to delete files or directories 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 delete operation is completed.

fileRemoveSync

The fileRemoveSync function allows deleting the file or folder specified by <relativePath> in the predefined folder. If a folder is addressed, the parameter <recursive> allows specifying whether each file and directory contained in <relativePath> must be deleted recursively.

fileRemove

The fileRemove function allows deleting asynchronously the file or folder specified by <relativePath> in the predefined folder. If a folder is addressed, the parameter <recursive> allows specifying to delete recursively every file and directory contained in <relativePath>.

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

Syntax

 

(Synchronous remove)

var result = fileRemoveSync(<relativePath>, <recursive>)

 

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

fileRemove(<relativePath>, <callback>, <recursive>)

Parameters Usage

Parameter

Type

Default

Use

Annotation

relativePath

String

-

Mandatory

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

recursive

Boolean

False

Optional

This parameter is meaningful only when deleting folders:

  • True to delete the specified directory, its subdirectories, and all files recursively.
  • False to delete the specified directory only if empty.

The callback function is declared as follows:

 

function fileRemoveCallback(<FileRemoveResult>)

{

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

}

 

Where <FileRemoveResult> is the result object.

If <recursive> is False and the directory is not empty, the operation fails. If <relativePath> specifies a file, this parameter is ignored.

Result

The functions fileRemoveSync and fileRemoveCallback return the FileRemoveResult object.

Error Handling

Errors can occur in case:

Examples of Use

 

How to delete an existing file

var result = fileRemoveSync("MyFolder\MyFile.txt")

console("Error = '{0}'", result.error)

 

If "MyFile.txt" exists, the code in the example prints to the Console expander the following:

Error = ''

and deletes the specified file.

 

How to delete an existing folder, only if it is empty

var result = fileRemoveSync("MyFolder")

console("Error = '{0}'", result.error)

 

If "MyFolder" contains one or multiple files or directories, since the parameter <Recursive> is False by default, the code in the example does not delete "MyFolder" and prints to the Console expander the following:

Error = 'The directory '[Installation Drive]:\[Installation Folder]\[Project Name]\shared\scripting\MyFolder' is not empty.'

 

If "MyFolder" is empty, it is deleted without errors while the Console expander contains:

Error = ''

 

How to delete an existing folder, regardless of whether it is empty

The code in the following example deletes “MyFolder”, its subdirectories, and any files:

var result = fileRemoveSync("MyFolder", true)