'How to check if a directory exists and is not empty?

zip.loadAsync(files).then(function(directory:any){
if (directory.folder("Mary")){ // not sure what to do here
      console.log("fail");
    }
else{
directory.folder("Mary").forEach(function (filename: any) {Console.log(filename);});
    };
}

I try to check if the directory "Mary" does exist and is not null before I do something to every file inside "Mary". But I am not sure what to do.



Solution 1:[1]

you can import fs and use this

import * as fs from "fs";

fs.readdir("Mary", function(err, files) {
    if (err) {
       // some sort of error
    } else {
       if (!files.length) {
           // directory appears to be empty
       }
    }

Solution 2:[2]

function isNotEmptyDir(dirpath){
if(fs.lstatSync(dirpath).isDirectory()){
    const filepaths = fs.readdirSync(dirpath)
    console.log('filepaths :' + filepaths)
    if(filepaths.length > 0){
        return true
    }
}
return 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
Solution 2