This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/api/assets/utils/deleteFilesNameStartWith.js

20 lines
630 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)) {
console.log(name)
fs.unlinkSync(path.join(dirPath, name));
}
}
});
}
module.exports = deleteFilesNameStartWith;