Class: File

File(path)

Represents a folder or a file in the virtual file-system. This file/folder may not yet exist.

To read an existing file:

var myFile = new File("/mydir/myfile.txt");
var content = myFile.readText();

To create a new file and write to it:

var myFile = new File("/mydir/myfile.txt");
myFile.writeText("Hello world");

To get a list of files in a folder:

var myFolder = new File("/mydir");
var files = myFolder.getFiles();

To create a new folder:

var myFolder = new File("/mydir");
myFolder.createFolder();

To write an object to a JSON file and read it back again:

var obj1 = { name: "Fred" };
var objFile = new File("/mydir/myobject.json");
objFile.writeObject(obj1);
var obj2 = objFile.readObject();

Constructor

new File(path)

Creates an object that represents a folder or a file in the virtual file-system. The file may or may not already exist.

Parameters:
Name Type Description
path String

Path of file/folder in the virtual file-system.

Members

adapterAbsolutePath :String

The absolute path of the file in the adapter file-system (usually Windows).
For example, the adapterAbsolutePath of /Home is C:\ProgramData\Enterprise Distributed Technologies\Complete FTP\Users (by default).

Type:
  • String

canList :Boolean

Does the current user have permission to list this folder?

Type:
  • Boolean

canRead :Boolean

Does the current user have permission to read this file?

Type:
  • Boolean

canWrite :Boolean

Does the current user have permission to write to this file?

Type:
  • Boolean

extension :String

Extension of the file.

Type:
  • String

fullPath :String

The absolute path of the file in CompleteFTP's virtual file-system. For example, by default the fullPath of the home-folder of the user NewUser1 is /Home/NewUser1.

Type:
  • String

isExecutable :Boolean

Is this file an executable?

Type:
  • Boolean

isFile :Boolean

Is this a file?

Type:
  • Boolean

isFolder :Boolean

Is this a folder?

Type:
  • Boolean

isFolderEmpty :Boolean

Is this an empty folder? Throws an exception if it's not a folder.

Type:
  • Boolean

isLengthKnown :Boolean

Is the length of this file known?

Type:
  • Boolean

isVirtualFileSystemFolder :Boolean

Is this file a folder in the virtual file-system?

Type:
  • Boolean

length :Number

Length of the file in bytes.

Type:
  • Number

modifiedTime :Date

Date and time that this file/folder was last modified.

Type:
  • Date

name :String

The name of the file.

Type:
  • String

Methods

appendText(text) → {void}

Append the given text to this file.

Parameters:
Name Type Description
text String

Text to append to the file.

Returns:
Type
void

copyTo(toPath)

Copy this file to the given destination.

Parameters:
Name Type Description
toPath String

Path to copy this file to.

createFile()

Create an empty file at the location defined by this File object.

createFolder()

Create a folder at the path represented by this File object.

exists() → {Boolean}

Does this file or folder exist?

Returns:
Type
Boolean

getFiles() → {Array.<File>}

Returns an array of File objects representing the contents of this folder. Throws an exception if this File object is not a folder.

Returns:
Type
Array.<File>

getParent() → {File}

Return a File object representing the parent-folder of this File object.

Returns:
Type
File

moveTo(toPath)

Move this file to the given path.

Parameters:
Name Type Description
toPath String

Path to move this file to.

readBase64() → {String}

Reads the contents of the file and returns it as a base64 encoded string.
This only works for files up to 100kB.

Returns:

base64 encoded string containing contents of file.

Type
String

readLines(numLines) → {Array.<String>}

Returns the given number of lines (or all lines if numLines=0) as an array of strings.

Parameters:
Name Type Description
numLines
Returns:
Type
Array.<String>

readObject() → {Object}

Parses the contents of the file as JSON and returns a Javascript object. This only works for files up to 100kB.

Returns:
Type
Object

readText() → {String}

Returns the contents of this file as a string. This only works for files up to 100kB.

Returns:
Type
String

remove()

Delete the file or folder at the path represented by this File object.

removeFile()

Delete the file at the path represented by this File object. Fails if this is a folder.

removeFolder(recursiveopt)

Delete the folder at the path represented by this File object. Fails if this is a file.

Parameters:
Name Type Attributes Default Description
recursive String <optional>
false

Remove this folder.

rename(newName) → {File}

Rename the current file to the given name.

Parameters:
Name Type Description
newName String

New name of file.

Returns:

New File object for the renamed file.

Type
File

toURL() → {String}

Get the URL that this file is accessible at.

Returns:
Type
String

unzip(destinationFolder, overwrite)

Unzip this file to the destination folder.

Parameters:
Name Type Description
destinationFolder String

Virtual file-system path of folder where the file should be unzipped.

overwrite Boolean

If true then any existing files are overwritten otherwise an exception is thrown if a file is in the way.

writeBase64(base64String) → {void}

Parses the given base64-encoded string and writes the result to the file.

Parameters:
Name Type Description
base64String String

base64 encoding of what is to be written to the file.

Returns:
Type
void

writeObject(object) → {void}

Write the given object to the file in JSON format.

Parameters:
Name Type Description
object String

Object to write to the file.

Returns:
Type
void

writeText(text) → {void}

Write the given text to this file.

Parameters:
Name Type Description
text String

Text to write to the file.

Returns:
Type
void