Javascript fs.readdirSync,如何获取路径中的子文件夹? 如何获取子文件夹?
路径距离/docs/: 2006/Art1 2006/Art2 2008/Art1 离线 测验 常量distPath='dist/docs/'; 函数getDirectoriesdistPath{ 返回fs.readdirSyncdistPath.filterfunction文件{ 返回fs.statSyncdistPath+'/'+file.isDirectory; }.filterfunction distPath{ 返回distPath!=“测试”&&distPath!=“脱机”; }; } 让articlePath=getDirectoriesdistPath; 意料之外 ‘2006’、‘2006’、‘2008’ 期望 ‘2006/Art1’、‘2006/Art2’、‘2008/Art1’ fs.readdirSync只读取一个目录的内容;如果您发现一个条目是一个子目录,并且需要读取给定子目录的内容,那么还需要在该子目录上调用fs.readdirSync 看来你需要一些递归的东西Javascript fs.readdirSync,如何获取路径中的子文件夹? 如何获取子文件夹?,javascript,node.js,Javascript,Node.js,路径距离/docs/: 2006/Art1 2006/Art2 2008/Art1 离线 测验 常量distPath='dist/docs/'; 函数getDirectoriesdistPath{ 返回fs.readdirSyncdistPath.filterfunction文件{ 返回fs.statSyncdistPath+'/'+file.isDirectory; }.filterfunction distPath{ 返回distPath!=“测试”&&distPath!=“脱机”; };
function deepGetDirectories(distPath) {
return fs.readdirSync(distPath).filter(function (file) {
return fs.statSync(distPath + '/' + file).isDirectory();
}).reduce(function(all, subDir) {
return [...all, ...fs.readdirSync(distPath + '/' + subDir).map(e => subDir + '/' + e)]
}, []);
}
fs.readdirSync只读取一个目录的内容;如果您发现一个条目是一个子目录,并且需要读取给定子目录的内容,那么还需要在该子目录上调用fs.readdirSync
看来你需要一些递归的东西
function deepGetDirectories(distPath) {
return fs.readdirSync(distPath).filter(function (file) {
return fs.statSync(distPath + '/' + file).isDirectory();
}).reduce(function(all, subDir) {
return [...all, ...fs.readdirSync(distPath + '/' + subDir).map(e => subDir + '/' + e)]
}, []);
}
谢谢你的支持
函数getDirectoriesdistPath{
返回fs.readdirSyncdistPath.filterfunction文件{
返回fs.statSyncdistPath+'/'+file.isDirectory;
}.filterfunction distPath{
返回distPath!=“自动运行”和&distPath!=“脱机”;
}.reducefunction all,子目录{
return[…all,…fs.readdirSyncdistPath+'/'+subDir.mape=>subDir+'/'+e]
},[].filterfunction文件{
返回fs.statSyncdistPath+'/'+file.isDirectory;
};
}
让articlePath=getDirectoriesdistPath;
我使用了他的代码建议:
.reducefunction all,子目录{
return[…all,…fs.readdirSyncdistPath+'/'+subDir.mape=>subDir+'/'+e]
},[].filterfunction文件{
返回fs.statSyncdistPath+'/'+file.isDirectory;
};
谢谢你的支持
函数getDirectoriesdistPath{
返回fs.readdirSyncdistPath.filterfunction文件{
返回fs.statSyncdistPath+'/'+file.isDirectory;
}.filterfunction distPath{
返回distPath!=“自动运行”和&distPath!=“脱机”;
}.reducefunction all,子目录{
return[…all,…fs.readdirSyncdistPath+'/'+subDir.mape=>subDir+'/'+e]
},[].filterfunction文件{
返回fs.statSyncdistPath+'/'+file.isDirectory;
};
}
让articlePath=getDirectoriesdistPath;
我使用了他的代码建议:
.reducefunction all,子目录{
return[…all,…fs.readdirSyncdistPath+'/'+subDir.mape=>subDir+'/'+e]
},[].filterfunction文件{
返回fs.statSyncdistPath+'/'+file.isDirectory;
};