2020-04-07 17:15:01 +02:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
function deleteFilesNameStartWith(pattern, dirPath = __dirname) {
|
|
|
|
// Get all file names in directory
|
|
|
|
fs.readdir(path.resolve(dirPath), (err, fileNames) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log(dirPath);
|
|
|
|
// Iterate through the found file names
|
|
|
|
for (const name of fileNames) {
|
|
|
|
// If file name matches the pattern
|
2020-04-08 00:47:24 +02:00
|
|
|
if (name.startsWith(pattern) && name !== 'default.png') {
|
2020-04-07 17:15:01 +02:00
|
|
|
console.log(name)
|
|
|
|
fs.unlinkSync(path.join(dirPath, name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = deleteFilesNameStartWith;
|