Skip to content

Instantly share code, notes, and snippets.

@frenchbread
Created March 2, 2017 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frenchbread/53eeb0c9373abe4f1d9c9f8dca4ef642 to your computer and use it in GitHub Desktop.
Save frenchbread/53eeb0c9373abe4f1d9c9f8dca4ef642 to your computer and use it in GitHub Desktop.
Iterate through files in dir
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