Skip to content

Instantly share code, notes, and snippets.

@agrublev
Last active January 31, 2020 17:31
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 agrublev/f2ca9b3f90a9d6670de8a90789be5a87 to your computer and use it in GitHub Desktop.
Save agrublev/f2ca9b3f90a9d6670de8a90789be5a87 to your computer and use it in GitHub Desktop.
read files recursively Node Recursive File List
const fs = require("fs");
const path = require("path");
const walk = function(dir, done) {
let results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
let pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
};
const walkSync = async dir => {
return new Promise(resolve => {
walk(dir, (b, z) => resolve(z));
});
};
(async () => {
const z = await walkSync("./devops");
let zl = z.filter(e => e.includes(".php"));
for (let f in zl) {
let file = zl[f];
let fname = file.split("/").pop();
await fs.copyFileSync(file, "./backup/" + fname);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment