代码之家  ›  专栏  ›  技术社区  ›  panthro

检查是否存在多个文件夹?

  •  0
  • panthro  · 技术社区  · 8 年前

    我使用fs。stat检查文件夹是否存在:

    fs.stat('path-to-my-folder', function(err, stat) {
        if(err) {
            console.log('does not exist');
        }
        else{
            console.log('does exist');
        }
    });
    

    是否有一种方法只使用一种方法检查多条路径的存在?

    2 回复  |  直到 8 年前
        1
  •  2
  •   samanime    8 年前

    fs

    function checkIfAllExist (paths) {
      return Promise.all(
        paths.map(function (path) {
          return new Promise(function (resolve, reject) {
            fs.stat(path, function (err, stat) {
              err && reject(path) || resolve()
            });
          });
        }))
      );
    };
    

    您可以这样使用它:

    checkIfAllExist([path1, path2, path3])
      .then(() => console.log('all exist'))
      .catch((path) => console.log(path + ' does not exist')
    

        2
  •  1
  •   Michał Perłakowski    8 年前

    不,文件系统API没有检查是否存在多个文件夹的功能。你只需要打电话给 fs.stat() 函数多次运行。