Skip to content

Instantly share code, notes, and snippets.

@ternavsky
Created December 8, 2020 16:56
Show Gist options
  • Save ternavsky/8d33a5fda14ce49754d1e754757a2815 to your computer and use it in GitHub Desktop.
Save ternavsky/8d33a5fda14ce49754d1e754757a2815 to your computer and use it in GitHub Desktop.
Node.js read all files in folder and subfolders
const fs = require('fs');
const path = require('path');
const walkSync = (dir, callback) => {
const files = fs.readdirSync(dir);
files.forEach((file) => {
var filepath = path.join(dir, file);
const stats = fs.statSync(filepath);
if (stats.isDirectory()) {
walkSync(filepath, callback);
} else if (stats.isFile()) {
callback(dir, file, filepath, stats);
}
});
};
console.log("Start processing..");
const myArgs = process.argv.slice(2);
const dir = myArgs[0];
walkSync(dir, (dir, file, filepath, stats) => {
// TODO actions with file
console.log("File", filepath, "processed");
});
console.log("All data processed successfully!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment