|
|
|
@@ -0,0 +1,39 @@ |
|
|
|
//filechecker by Jan Svabenik |
|
|
|
// *** ATTN: This code deletes files! *** |
|
|
|
|
|
|
|
//requiring path and fs modules |
|
|
|
const Fs = require('fs'); |
|
|
|
const path = require('path'); |
|
|
|
const actualdate = Date.now(); |
|
|
|
const ticksinday = 86000000; |
|
|
|
const maxfileage = 10; |
|
|
|
//joining path of directory |
|
|
|
|
|
|
|
const directoryPath = path.join('./', ''); |
|
|
|
|
|
|
|
|
|
|
|
function createdDate(file) { |
|
|
|
const { |
|
|
|
birthtime |
|
|
|
} = Fs.statSync(file) |
|
|
|
|
|
|
|
return birthtime.getTime(); |
|
|
|
} |
|
|
|
|
|
|
|
//passing directoryPath and callback function |
|
|
|
|
|
|
|
Fs.readdir(directoryPath, function(err, files) { |
|
|
|
//handling error |
|
|
|
if (err) { |
|
|
|
return console.log('Unable to scan directory: ' + err); |
|
|
|
} |
|
|
|
//listing all files using forEach |
|
|
|
files.forEach(function(file) { |
|
|
|
// Do whatever you want to do with the file |
|
|
|
filedate = createdDate(directoryPath + '/' + file); |
|
|
|
fileage = ((actualdate - filedate) / ticksinday).toFixed(0); |
|
|
|
console.log(file + ' Age: ' + fileage + ' Days'); |
|
|
|
if (fileage > maxfileage) console.log(file + ' eligible for removal.'); |
|
|
|
|
|
|
|
}); |
|
|
|
}); |