I have an application that creates mp4 video files 24/7, to do cleaning these files I need to delete all unmodified files in the last 30 days for example.
How to do this in Node Js? I already looked for references and did not find.
I have an application that creates mp4 video files 24/7, to do cleaning these files I need to delete all unmodified files in the last 30 days for example.
How to do this in Node Js? I already looked for references and did not find.
You can use the fs.stat
method. This method provides a fs.Stats
object / class that has information about the date of creation, date of last modification, and other characteristics of the file.
To use you have to read all the files you have, filter the extension you want, and run one by one.
An example might look like this:
const fsReadDirRecGen = require('fs-readdir-rec-gen')
const quatroMesesAtras = (d => {
d.setMonth(d.getMonth() - 4);
return d;
})(new Date());
function filtrarPorExtensao(fileName) {
return fileName.endsWith('.mp4');
};
for (let file of fsReadDirRecGen('./test/testData', filtrarPorExtensao)) {
const Stats = fs.statSync(file);
if (Stats.mtime < quatroMesesAtras) {
// apagar o ficheiro
fs.unlinkSync(file)
}
}
Obs: I have not tested, but I have now assembled with logic that seems to me to be accurate.