Last active
January 31, 2020 17:31
-
-
Save agrublev/f2ca9b3f90a9d6670de8a90789be5a87 to your computer and use it in GitHub Desktop.
read files recursively Node Recursive File List
This file contains hidden or 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 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