Skip to content

Instantly share code, notes, and snippets.

@lichenbuliren
Created June 16, 2016 02:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lichenbuliren/94c777e3e0b5c3a43732fec7af785fd6 to your computer and use it in GitHub Desktop.
Save lichenbuliren/94c777e3e0b5c3a43732fec7af785fd6 to your computer and use it in GitHub Desktop.
/**
* 遍历文件夹目录
* @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