JavaScript读取指定目录下所有文件路径并输入(代码片段)

const fs = require("fs");
const path = require("path");

/**
 * 读取指定路径下的所有文件路径并赋值到out中
 * @param {string} parentPath 
 * @param {Array} out 
 */
function readAll(parentPath, out) {
    try{
        let files = fs.readdirSync(parentPath);
        files.forEach(function(item){
            let tempPath = path.join(parentPath,item);
            let stats = fs.statSync(tempPath);
            if(stats.isDirectory()){
                readAll(tempPath,out);
            }else{
                out.push(tempPath);
            }
        });
    }catch(e){
        console.warn("Path Error:" + parentPath);
        return out;
    }
}

你可能感兴趣的:(代码片段)