Count the number of files in a directory using JavaScript/nodejs?

前端 未结 7 1601
星月不相逢
星月不相逢 2021-02-12 18:04

How can I count the number of files in a directory using nodejs with just plain JavaScript or packages? I want to do something like this:

How to count the n

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-12 18:36

    const readdir = (path) => {
      return new Promise((resolve, reject) => {
        fs.readdir(path, (error, files) => {
          error ? reject(error) : resolve(files);
        });
      });
    };s
    
    readdir("---path to directory---").then((files) => {
      console.log(files.length);
    });
    

提交回复
热议问题