/**
* Read all files from build/ folder recursively
*/
function readDir(path){
let sub = fs.readdirSync(path)
// ignore hide files
sub = sub.filter((e)=>{
return e.indexOf('.') !== 0
})
// complete path
sub = sub.map((e)=>{
return _path.resolve(path, e)
})
if(sub.length === 0){
console.log('😫 build文件夹下没有文件!'.error)
}else{
return sub
}
}
function readPath(path){
// Determine whether the path is dir or not
let stat = fs.statSync(path)
if(stat.isFile()){
S3_files.push(path)
}else if(stat.isDirectory()){
let sub_children = readDir(path)
sub_children.forEach((e)=>{
readPath(e)
})
}
}
readPath(BUILD_PATH)
查
fs对象下的 API 就好了诶没办法 ,自己写了一个递归。诶 这 用到了这几个api var fs1 = require(‘fs’); stats = fs1.lstatSync(filePath);//查看文件属性 fs1.readdir();罗列文件夹下面文件, 需要的可以参考下
靠谱
《了不起的node.js》 上的代码
嗯哼 这书还不赖
var fs = require(“fs”);
var path = “d:\WinRAR”;
function explorer(path){ fs.readdir(path, function(err,files){ if(err){ console.log(“error:\n”+err); return; }
files.forEach(function(file){ fs.stat(path+"\"+file+’’,function(err,stat){ if(err){ console.log(err); return; }
}
explorer(path);
我自己写的 贴进来格式有点变化,markdown还不太会用
5楼正解~!
这个也不错!https://github.com/coolaj86/node-walk
var fs = require(‘fs’), util = require(‘util’), path = ‘D:/mp3’;
function explorer(path){
}
explorer(path);
ndir模块做了这件事
fs.readdir
可以参考npm模块glob的实现,
glob可以使用与shell模式匹配一样的方式遍历文件目录树
或者自己用递归的方式实现
glob
Maybe the follow is what you want :)