Created
December 5, 2012 04:46
-
-
Save adamwdraper/4212319 to your computer and use it in GitHub Desktop.
Loop through all files in a given directory with node.js
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
var fs = require('fs'); | |
var walkPath = './'; | |
var walk = function (dir, done) { | |
fs.readdir(dir, function (error, list) { | |
if (error) { | |
return done(error); | |
} | |
var i = 0; | |
(function next () { | |
var file = list[i++]; | |
if (!file) { | |
return done(null); | |
} | |
file = dir + '/' + file; | |
fs.stat(file, function (error, stat) { | |
if (stat && stat.isDirectory()) { | |
walk(file, function (error) { | |
next(); | |
}); | |
} else { | |
// do stuff to file here | |
console.log(file); | |
next(); | |
} | |
}); | |
})(); | |
}); | |
}; | |
// optional command line params | |
// source for walk path | |
process.argv.forEach(function (val, index, array) { | |
if (val.indexOf('source') !== -1) { | |
walkPath = val.split('=')[1]; | |
} | |
}); | |
console.log('-------------------------------------------------------------'); | |
console.log('processing...'); | |
console.log('-------------------------------------------------------------'); | |
walk(walkPath, function(error) { | |
if (error) { | |
throw error; | |
} else { | |
console.log('-------------------------------------------------------------'); | |
console.log('finished.'); | |
console.log('-------------------------------------------------------------'); | |
} | |
}); |
hi. I put the files into an array, and I want to return this array to another function?
How to do this.
Add your logic on line 29
works completely, thanks!
Beautiful code. Thank you!
ur the fucking best
i want unzip files if zipped how to do it
awesome
added support to loop through files within subdirectories of that given directory here https://github.com/gavofyork/entropretty/pull/19/files
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi. I put the files into an array, and I want to return this array to another function?
How to do this.