node.js 读取文件目录下的所有文件,JS读取文件目录
文件目录结构如下图:
代码1.js:
进入test目录:
进入ch目录:
1.js:
var fs = require('fs');
var join = require('path').join;
function getJsonFiles(jsonPath){
let jsonFiles = [];
function findJsonFile(path){
let files = fs.readdirSync(path);
files.forEach(function (item, index) {
let fPath = join(path,item);
let stat = fs.statSync(fPath);
if(stat.isDirectory() === true) {
findJsonFile(fPath);
if (stat.isFile() === true) {
jsonFiles.push(fPath);