How to check if a directory exists in Cordova?

Viewed 1902

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?

2 Answers

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

Related