吴锴的博客

Life? Don't talk to me about life!

0%

使用 Nodejs 遍历文件夹

最近在写一个管理 markdown 文件的工具 knowledge-center,需要读取指定文件夹内所有 markdown 文件。因此需要用 Node.js 来实现遍历一个文件夹内所有文件的功能。

Node.js 中提供了这些有用的 API:

  • fs.readdir:异步读取文件夹
  • fs.readdirSync:同步读取文件夹
  • fs.statSync:同步获取文件属性

获取的文件列表为数组格式

对于遍历的结果,我们可以选择按列表或文件树来展示。先从最简单的情况看起,用同步方式处理,返回结果是一个列表。

const fs = require('fs');
const path = require('path');
/**
* 以同步方式遍历,返回文件的列表
* @param {*} rootPath
* @param {*} list
* @returns
*/
const traverseFolderList = (rootPath, list) => {
const files = fs.readdirSync(rootPath);
files.forEach((file) => {
const absolutePath = path.resolve(rootPath, file);
const stats = fs.statSync(absolutePath);
if (stats.isFile()) {
list.push(absolutePath);
}
if (stats.isDirectory()) {
traverseFolderList(absolutePath, list);
}
});
return list;
};
module.exports = { traverseFolderList };

先使用 fs.readdirSync 获取文件列表,然后遍历文件列表,使用 fs.statSync 获取列表中文件的状态,如果是文件,则添加到文件列表中,如果是文件夹,则递归调用 traverseFolderList 函数,直到获取到所有文件。

获取的文件列表为对象格式

如果我们想展示文件夹目录结构,那么列表格式的就不太方便了。假设有如下的文件夹结构:

1
2
3
4
5
6
7
8
./1
├── 2
│ ├── test2.txt
│ └── test2_1.txt
└── 3
├── 4
│ └── test4.txt
└── test3.txt

希望获取到的对象结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
root: {
path: './1',
type: 'folder',
children: ['2', '3'],
isRoot: true,
},
2: {
path: '2',
type: 'folder',
children: ['2/test2.txt', '2/test2_1.txt'],
},
3: { path: '3', type: 'folder', children: ['3/4', '3/test3.txt'] },
'2/test2.txt': { path: '2/test2.txt', type: 'file' },
'2/test2_1.txt': { path: '2/test2_1.txt', type: 'file' },
'3/4/test4.txt': { path: '3/4/test4.txt', type: 'folder', children: [] },
'3/4': { path: '3/4', type: 'folder', children: ['3/4/test4.txt'] },
'3/test3.txt': { path: '3/test3.txt', type: 'file' },
};

这个对象以文件/文件夹相对于根目录的相对路径为 key,每个节点包含了这些属性:

  • type:用于区分文件或文件夹类型
  • path:相对路径
  • children:如果是文件夹类型,则其中是子文件的相对路径
const fs = require('fs');
const path = require('path');
/**
* 以同步方式遍历,返回文件树结构的对象
* @param {Object} param0
* @param {string} param0.rootPath 这个文件夹的根目录
* @param {string} param0.folderRelativePath 正在遍历的这个文件夹相对于根目录的相对路径
* @param {*} param0.nodes 要保存的数据内容,以相对路径为 key
* @param {*} param0.parentNode 当前文件的父级文件夹,遍历时需要修改 parent 的 children 属性
* @returns
*/
const traverseFolderObj = ({
rootPath,
folderRelativePath,
nodes,
parentNode,
}) => {
const files = fs.readdirSync(path.resolve(rootPath, folderRelativePath));
files.forEach((file) => {
const absolutePath = path.resolve(rootPath, folderRelativePath, file);
const relativePath = path.relative(rootPath, absolutePath);
const stats = fs.statSync(absolutePath);
if (stats.isFile()) {
nodes[relativePath] = {
path: relativePath,
type: 'file',
};
parentNode.children.push(relativePath);
}
if (stats.isDirectory()) {
const newParent = {
path: relativePath,
type: 'folder',
children: [],
};
traverseFolderObj({
rootPath,
folderRelativePath: relativePath,
nodes,
parentNode: newParent,
});
nodes[relativePath] = newParent;
parentNode.children.push(relativePath);
}
});
return nodes;
};
module.exports = { traverseFolderObj };

异步方式

在上面的实现中,都是使用了同步的方式来处理,即 fs.readdirSync 方法,可以使用异步方式来处理吗?

可以选择 fs.readdir 来异步读取文件夹, 但是回调函数的调用方式不太方便。在 Node 10+ 中提供了 fs.promises API,其中提供了一些文件系统的方法,它们返回的是一个 Promise 对象,而非使用回调函数。这里可以从 fs.promises 中引入 readdir 方法,从而可以使用方便的 async/await 语法来进行异步处理,避免了回调函数的方式。

1
const { readdir } = require('fs').promises; 

将上面的 traverseFolderList 方法重写为异步格式:

/** 异步遍历 */
const fs = require('fs');
const path = require('path');
const { readdir } = require('fs').promises;
const asyncTraverseFolderList = async (rootPath) => {
const files = await readdir(rootPath);
const list = await Promise.all(
files.map(async (file) => {
const absolutePath = path.resolve(rootPath, file);
const stats = fs.statSync(absolutePath);
if (stats.isFile()) {
return absolutePath;
}
return asyncTraverseFolderList(absolutePath);
})
);
return Array.prototype.concat(...list);
};
module.exports = { asyncTraverseFolderList };

比较同步和异步两种方案

traverseFolderListasyncTraverseFolderList 返回的结果都是列表格式,我们可以写一个测试脚本来比较下二者的运行时间:

const os = require('os');
const path = require('path');
const { traverseFolderList } = require('./traverseFolderList');
const { asyncTraverseFolderList } = require('./asyncTraverseFolderList');
// 可以任选一个文件夹用于测试
const folderPath = path.resolve(os.homedir(), 'code');
function testSync() {
const list = [];
const startTime = Date.now();
traverseFolderList(folderPath, list);
const endTime = Date.now();
const duration = endTime - startTime;
console.log(`同步 - 共${list.length}个文件, 耗时${duration}ms`);
return duration;
}
async function testAsync() {
const startTime = Date.now();
const list = await asyncTraverseFolderList(folderPath);
const endTime = Date.now();
const duration = endTime - startTime;
console.log(`异步 - 共${list.length}个文件, 耗时${duration}ms`);
return duration;
}
async function runTest(fn, count) {
let time = 0;
for (let i = 0; i < count; i++) {
time += await fn();
}
console.log(`${fn.name} 共运行${count}次,平均耗时:${time / count}ms`);
}
(async function () {
const COUNT = 10;
await runTest(testSync, COUNT);
await runTest(testAsync, COUNT);
})();
view raw index.js hosted with ❤ by GitHub

分别用两个函数遍历了同一个文件夹十次后,统计结果如下,异步方式比同步方式减少了约18%的时间。

1
2
同步 - 平均耗时:1217.1ms
异步 - 平均耗时:1025.7ms

注意一点,本文中的代码都是没有做错误处理的,实际上读取文件时可能会出错,因此将相应的代码使用 try...catch 包起来是一个合理的做法。

参考资料

https://stackoverflow.com/a/45130990

fs.promises API

https://javascript.info/promisify

如果我的文章对你有帮助,欢迎打赏支持!

欢迎关注我的其它发布渠道

Gitalking ...