files/en-us/web/api/filesystementry/getparent/index.md
{{APIRef("File and Directory Entries API")}}
The {{domxref("FileSystemEntry")}} interface's method
getParent() obtains a
{{domxref("FileSystemDirectoryEntry")}}.
getParent(successCallback, errorCallback)
getParent(successCallback)
successCallback
errorCallback {{optional_inline}}
None ({{jsxref("undefined")}}).
DOMException.INVALID_STATE_ERR
DOMException.NOT_FOUND_ERR
DOMException.SECURITY_ERR
This example renames the file specified by the variable fileEntry to
"newname.html".
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 {{domxref("FileSystemDirectoryEntry")}} object representing the directory the file is currently located in. Then {{domxref("FileSystemEntry.moveTo", "moveTo()")}} is used to rename the file within that directory.
Currently, there isn't a {{jsxref("Promise")}}-based version of this method. You can, however, create a simple helper function to adapt it, like this:
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}}
{{Compat}}