Przeglądaj źródła

„filecheck.js“ ändern

master
Jan Svabenik 4 lat temu
rodzic
commit
26d63bcf7c
1 zmienionych plików z 21 dodań i 10 usunięć
  1. +21
    -10
      filecheck.js

+ 21
- 10
filecheck.js Wyświetl plik

@@ -1,16 +1,12 @@
//filechecker by Jan Svabenik
// *** ATTN: This code deletes files! ***

//requiring path and fs modules
//requiring some constants 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('./', '');

//joining path of directory
const directoryPath = path.join('C:/Test', '');

function createdDate(file) {
const {
@@ -27,13 +23,28 @@ Fs.readdir(directoryPath, function(err, files) {
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

// Print File Age & Remove it, if its older than allowed.

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.');

if (fileage > maxfileage) {
console.log(file + ' eligible for removal.');

Fs.unlink(directoryPath + '/' + file, (err) => {
if (err) {
console.error(err)
return
}

//file removed
console.log(file + ' removed.');
})
}
});
});

Ładowanie…
Anuluj
Zapisz