I need to delete all files in a directory except for one that I have the name of. Let's say:
fs.unlink('./all except specialfile.txt', (err)) => { if (err) throw err; console.log('file deleted!'); } I need to delete all files in a directory except for one that I have the name of. Let's say:
fs.unlink('./all except specialfile.txt', (err)) => { if (err) throw err; console.log('file deleted!'); } You have to get all files in the directory and then compare the name and delete the file if file name does not match your file
const fs = require('fs'); const path = require('path'); fs.readdir('./', (err, files) => { if (err) { console.log(err); } files.forEach(file => { const fileDir = path.join('./', file); if (file !== 'specialfile.txt') { fs.unlinkSync(fileDir); } }); });