1
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.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Anee go
  • 65
  • 5

2 Answers2

0

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
       }
    }
Thibaud
  • 1,059
  • 4
  • 14
  • 27
-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

}

  • 1
    [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Stephen Ostermiller Mar 23 '22 at 15:19
  • fs is a nodejs library and it cannot be used by Angular applications. Once the code compiles it will throw an error stating that it cannot resolve the fs module, doing [this](https://stackoverflow.com/a/63427821/9333976) does not help either. So, I believe this has to be solved on a different layer rather than the front-end. – Nahuel Leiva Aug 02 '22 at 15:23