Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

39 lignes
1.0KB

  1. //filechecker by Jan Svabenik
  2. // *** ATTN: This code deletes files! ***
  3. //requiring path and fs modules
  4. const Fs = require('fs');
  5. const path = require('path');
  6. const actualdate = Date.now();
  7. const ticksinday = 86000000;
  8. const maxfileage = 10;
  9. //joining path of directory
  10. const directoryPath = path.join('./', '');
  11. function createdDate(file) {
  12. const {
  13. birthtime
  14. } = Fs.statSync(file)
  15. return birthtime.getTime();
  16. }
  17. //passing directoryPath and callback function
  18. Fs.readdir(directoryPath, function(err, files) {
  19. //handling error
  20. if (err) {
  21. return console.log('Unable to scan directory: ' + err);
  22. }
  23. //listing all files using forEach
  24. files.forEach(function(file) {
  25. // Do whatever you want to do with the file
  26. filedate = createdDate(directoryPath + '/' + file);
  27. fileage = ((actualdate - filedate) / ticksinday).toFixed(0);
  28. console.log(file + ' Age: ' + fileage + ' Days');
  29. if (fileage > maxfileage) console.log(file + ' eligible for removal.');
  30. });
  31. });