Created
March 2, 2017 05:56
-
-
Save frenchbread/53eeb0c9373abe4f1d9c9f8dca4ef642 to your computer and use it in GitHub Desktop.
Iterate through files in dir
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
const fs = require('fs') | |
var walk = function (dir, done) { | |
fs.readdir(dir, (error, list) => { | |
if (error) { | |
return done(error) | |
} | |
let i = 0; | |
(function next () { | |
let file = list[i++] | |
if (!file) { | |
return done(null) | |
} | |
file = `${dir}/${file}` | |
fs.stat(file, (error, stat) => { | |
if (stat && stat.isDirectory()) { | |
walk(file, error => { | |
next() | |
}) | |
} else { | |
if (typeof file !== 'undefined') { | |
console.log(file) | |
} | |
next() | |
} | |
}) | |
}()) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment