-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirWalker.js
39 lines (32 loc) · 991 Bytes
/
dirWalker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var fs = require('fs');
/*
递归处理文件,文件夹
path 路径
floor 层数
handleFile 文件,文件夹处理函数
*/
function walk(path, floor, handleFile) {
handleFile(path, floor);
floor++;
fs.readdir(path, function(err, files) {
if (err) {
console.log('read dir error');
} else {
files.forEach(function(item) {
var tmpPath = path + '/' + item;
fs.stat(tmpPath, function(err1, stats) {
if (err1) {
console.log('stat error');
} else {
if (stats.isDirectory()) {
walk(tmpPath, floor, handleFile);
} else {
handleFile(tmpPath, floor);
}
}
})
});
}
});
}
exports.walk = walk;