Skip to content

Instantly share code, notes, and snippets.

@kethinov
Created September 22, 2013 09:04
Show Gist options
  • Save kethinov/6658166 to your computer and use it in GitHub Desktop.
Save kethinov/6658166 to your computer and use it in GitHub Desktop.
List all files in a directory in Node.js recursively in a synchronous fashion
// List all files in a directory in Node.js recursively in a synchronous fashion
var walkSync = function(dir, filelist) {
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
if (fs.statSync(dir + file).isDirectory()) {
filelist = walkSync(dir + file + '/', filelist);
}
else {
filelist.push(file);
}
});
return filelist;
};
@ghostfreak3000
Copy link

wow.. been receiving notifications for this thing for 9 years!.. it's the most consistent item in my dev career

@Skhmt
Copy link

Skhmt commented Mar 13, 2025

There are only 3 hard problems in computer science... cache invalidation, naming things, off by one errors, and directory walking,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment