From 26d63bcf7c5e4817a76132fa61d2d6245ea74582 Mon Sep 17 00:00:00 2001 From: Jan Svabenik Date: Sun, 2 May 2021 13:55:59 +0000 Subject: [PATCH] =?UTF-8?q?=E2=80=9Efilecheck.js=E2=80=9C=20=C3=A4ndern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- filecheck.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/filecheck.js b/filecheck.js index 8bd02d4..19f5422 100644 --- a/filecheck.js +++ b/filecheck.js @@ -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.'); + }) + } }); }); \ No newline at end of file