如何使用 nodejs/expressjs 删除目录中超过 6 个月的文件?

问题描述

这里我不是在谈论 temp 文件目录。 是关于我的项目。

这是以下 structure 我试图从每个登录我网站的用户的目录中关注到 delete the files

  1. 用户automatic email alert 之前收到 nodemailer(我使用的是 one week of 6 months finish)作为通知文件将在 a week 之后被删除,因此他可以采取如果他愿意,可以备份。
  2. 第二个 automatic email alert(我正在使用 nodemailer),就在 one day of 6 months finish 之前,明天文件将被删除
  3. 最后一天,(在总共 6 months 之后)文件将被自动成功删除

我尝试的代码是使用 setTimeoutunlink 但由于 max timesetTimeout 我们可以放的是 approx 24 days 而不是更多

那么有什么可能的方法来做到这一点?

解决方法

                                          var y,m,d,h,mi,da = new Date();
                                          y = da.getFullYear();
                                          da.setMonth(da.getMonth() + 5);
                                          m = da.getMonth();
                                          d = da.getDate();
                                          h = da.getHours();
                                          mi = da.getMinutes();
                                          console.log("month = " + m);
                                         var date = new Date(y,0);
                                          console.log(date);
                                         var j = schedule.scheduleJob(date,function(){
                                      fs.unlink(___path,function(err,success){
                                             if(err)
                                             {
                                                console.log(err);
                                                return res.end('{"msg" : "Unable to unlink the Bigpaths4Clients files : Rejected","status" : 700}');
                                             }
                                             else {
                                               User.updateMany({"Addtasks.commonID":req.query.commonIDs},{$set: {"Addtasks.$.background" :'linear-gradient(45deg,#D70652,#FF025E)',"Addtasks.$.status" :'Deleted'}},function (error,success) {
                                                     if (error) {
                                                       console.log("error = "+ error );
                                                     }
                                                     else {
                                                       console.log('file deleted successfully and status set as Deleted');
                                                     }
                                                   })
                                               }
                                             })
                                         }); //scheduler closed