Skip to content

Instantly share code, notes, and snippets.

@ivanjeremic
Created December 15, 2023 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanjeremic/d5d6db7c2ae16faa70064dd237bf8344 to your computer and use it in GitHub Desktop.
Save ivanjeremic/d5d6db7c2ae16faa70064dd237bf8344 to your computer and use it in GitHub Desktop.
remove file in folder based on condition nodeJS
const fs = require("fs");
const path = require("path");
const directory = path.join(__dirname, "public", "images");
fs.readdir(directory, (error, files) => {
if (error) throw new Error("Could not read directory");
files.forEach((file) => {
const file_path = path.join(directory, file);
if(file.includes("Zone.")) {
console.log(file);
fs.unlink(file_path, (error) => {
if (error) throw new Error("Could not delete file");
console.log(`Deleted ${file_path}`);
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment