node.js 讀取檔案目錄下的所有檔案,JS讀取檔案目錄
阿新 • • 發佈:2018-12-22
檔案目錄結構如下圖:
程式碼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); } }); } findJsonFile(jsonPath); console.log(jsonFiles); } getJsonFiles("test");
執行結果: