如何使用nodejs

时间:2019-09-22 19:36:46

标签: javascript node.js linux filesystems

我有一个路径,其中有成千上万个文件夹,我只想在该路径下读取一个文件夹名称。如何使用Node.js从某个路径中仅获取一个文件夹(子目录)名称。我不想读取所有目录并读取返回数组的第0个索引,也不希望在读取一个元素后中断循环,因为这将不必要地加载成千上万个文件夹名称。

  1. 尝试读取该路径下的所有目录,并读取返回数组的第0个索引。
  2. 尝试读取一个文件名后打破循环。 两者都有效,但不是最佳解决方案。
const fs = require('fs');
const fsPromise = fs.promises;

// inside some async function
/* 
Next statement will return all the subdirectory in FOLDER_PATH,
which is say hundreds in some case. 
But I'have to use only one of them. So why to fetch all and filter one?
*/
let subFolder = await fsPromise.readdir(FOLDER_PATH); 
let oneFolder = subFolder[0]; // read 0 index out of all the folder list returned.

谢谢!

0 个答案:

没有答案