Created
December 15, 2023 20:44
-
-
Save ivanjeremic/d5d6db7c2ae16faa70064dd237bf8344 to your computer and use it in GitHub Desktop.
remove file in folder based on condition nodeJS
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 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