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?
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);
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);
});
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With