Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 878
Pier Avatar asked Sep 13 '25 16:09

Pier


2 Answers

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);
like image 198
Pier Avatar answered Sep 16 '25 05:09

Pier


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);
                });
        });
    }

like image 41
Mirko Avatar answered Sep 16 '25 06:09

Mirko