20 lines
656 B
JavaScript
20 lines
656 B
JavaScript
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
|
|
if (name.startsWith(pattern) && name !== 'default.png') {
|
|
console.log(name)
|
|
fs.unlinkSync(path.join(dirPath, name));
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = deleteFilesNameStartWith; |