Search code examples
capacitorcapacitor-plugin

@capacitor/filesystem: How to check if a folder/directory exists or not?


In my ionic capacitor app, I need to check if a folder exists or not on my device. But I have not found any method in the FileSystem plugin. Although there is a method for reading all the contents inside a directory.

In my case, at first, I need to check if the directory already exists or not. If it is not found, then I will create the directory.


Solution

  • In try block write the code for creating a directory. An exception will be thrown otherwise.

        try {
          let ret = await Filesystem.mkdir({
            path: folderName,
            directory: FilesystemDirectory.Documents,
            recursive: false,
          });
          console.log("folder ", ret);
        } catch (e) {
          //console.error("Unable to make directory", e);
        }