Created
June 16, 2016 02:42
-
-
Save lichenbuliren/94c777e3e0b5c3a43732fec7af785fd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 遍历文件夹目录 | |
* @param {[type]} dir [description] | |
* @param {Function} callback [description] | |
* @return {[type]} [description] | |
*/ | |
function travel(dir, callback) { | |
var fileList = [], | |
folderList = []; | |
var walk = function(filePath, fileList, folderList) { | |
fs.readdirSync(filePath).forEach(function(file) { | |
var pathname = path.join(filePath, file); | |
var stats = fs.statSync(pathname); | |
if (stats.isDirectory()) { | |
walk(pathname, fileList, folderList); | |
folderList.push(pathname); | |
} else { | |
fileList.push(pathname); | |
} | |
}); | |
} | |
walk(dir, fileList, folderList); | |
callback && callback(fileList, folderList); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment