nodejs递归读取所有文件
# nodejs递归读取所有文件
var fs = require('fs');
var path = require('path');
function readFileList(dir, filesList = []) {
const files = fs.readdirSync(dir);
console.log(files);
files.forEach((item, index) => {
var fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
readFileList(path.join(dir, item), filesList); //递归读取文件
} else {
filesList.push(fullPath);
}
});
return filesList;
}
var filesList = [];
readFileList(__dirname,filesList);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21