Created
December 8, 2020 16:56
-
-
Save ternavsky/8d33a5fda14ce49754d1e754757a2815 to your computer and use it in GitHub Desktop.
Node.js read all files in folder and subfolders
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'); | |
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