FileSystemEntry: getParent() method

The FileSystemEntry interface's method getParent() obtains a FileSystemDirectoryEntry.

Syntax

js
getParent(successCallback, errorCallback)
getParent(successCallback)

Parameters

successCallback

A function which is called when the parent directory entry has been retrieved. The callback receives a single input parameter: a FileSystemDirectoryEntry object representing the parent directory. The parent of the root directory is considered to be the root directory, itself, so be sure to watch for that.

errorCallback Optional

An optional callback which is executed if an error occurs. There's a single parameter: a DOMException describing what went wrong.

Return value

None (undefined).

Exceptions

DOMException.INVALID_STATE_ERR

The operation failed because the file system's state doesn't permit it. This can happen, for example, if the file system's cached state differs from the actual state of the file system.

DOMException.NOT_FOUND_ERR

The specified path could not be found.

DOMException.SECURITY_ERR

Security restrictions prohibit obtaining the parent directory's information.

Examples

This example renames the file specified by the variable fileEntry to "newname.html".

js
fileEntry.getParent(
  (parent) => {
    fileEntry.moveTo(parent, "newname.html", (updatedEntry) => {
      console.log(`File ${fileEntry.name} renamed to newname.html.`);
    });
  },
  (error) => {
    console.error(
      `An error occurred: Unable to rename ${fileEntry.name} to newname.html.`,
    );
  },
);

This is accomplished by first obtaining a FileSystemDirectoryEntry object representing the directory the file is currently located in. Then moveTo() is used to rename the file within that directory.

Using promises

Currently, there isn't a Promise-based version of this method. You can, however, create a simple helper function to adapt it, like this:

js
function getParentPromise(entry) {
  return new Promise((resolve, reject) => {
    entry.getParent(resolve, reject);
  });
}

A similar approach can be taken elsewhere in the File and Directory Entries API.

Specifications

Specification
File and Directory Entries API
# dom-filesystementry-getparent

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
getParent
Chrome – Full support
Chrome 8 (Release date: 2010-12-02)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 52 (Release date: 2017-03-07)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 52 (Release date: 2017-03-07)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11.3 (Release date: 2018-03-29)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also