Created
March 19, 2018 02:42
-
-
Save seleb/ccdba97034c6820021bc57894df59db0 to your computer and use it in GitHub Desktop.
Deletes all files in a directory with a given extension
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 path = require("path"); | |
const fs = require("fs"); | |
function clearDir(dirPath, ext) { | |
fs.readdirSync(dirPath) | |
.filter(filePath => path.extname(filePath) === ext) | |
.forEach(filePath => { | |
filePath = path.join(dirPath, filePath); | |
try { | |
fs.unlinkSync(filePath); | |
} catch (e) { | |
console.error(e); | |
} | |
}) | |
}; | |
// example | |
clearDir(path.resolve("./output", ".png")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment