'How to check if a directory exists in Cordova?
I'm using window.resolveLocalFileSystemURL() to get a directory entry.
I need to know if this directory exists to delete it before proceeding.
When doing...
const path = cordova.file.dataDirectory + directoryName;
window.resolveLocalFileSystemURL(path, (directoryEntry) => {
console.log(directoryEntry);
});
... I get a DirectoryEntry object, but other than an empty name there doesn't seem to be a way to check whether it exists or not.
This is the only DirectoryEntry available documentation but it's seriously outdated:
https://cordova.apache.org/docs/en/2.4.0/cordova/file/directoryentry/directoryentry.html
The current docs of the File plugin don't have much information on DirectoryEntry:
https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html
So how can I know if a directory exists using Cordova?
Solution 1:[1]
My problem was that I didn't pass an error callback to window.resolveLocalFileSystemURL().
When passing it I get a FileError with code 1 equivalent to NOT_FOUND_ERR.
Sadly there is no documentation available for window.resolveLocalFileSystemURL and I had to resort to reading the source code.
The signature of the function is as follows:
window.resolveLocalFileSystemURL(path, successCallback, errorCallback);
Solution 2:[2]
If anyone still needs it
//check if a directory exists
async dirExists (absolutePath) {
return new Promise((resolve) => {
window.resolveLocalFileSystemURL(
absolutePath,
(dir) => {
console.log(dir);
resolve(true);
}, (error) => {
console.log(error);
resolve(false);
});
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pier |
| Solution 2 | Mirko |
