I want to delete a file named myfile with any extension.
const fs = require('fs')
const ext = '' ; //this extension may be anything
const path = './myfile.'+ext ;
fs.unlink(path, (err) => {
if (err) {
console.error(err)
return
}
//file removed
})
The error I get:
no such file or directory named myfile
But there is a file named myfile.jpg which I want to delete. Let's pretend that we don't know the extension. How can I delete it?
unlinkneeds to be given a specific path so you first have to find all the files to remove beforehand.array.filter(x=>x.match(/^myfile.*/))or if you are not familiar with regexparray.filter(x=>x.substr(0,7) === 'myfile.')). Then for each filename found delete it