首页 > 解决方案 > 如何使用 nodejs/expressjs 删除目录中超过 6 个月的文件?

问题描述

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

这是structure我试图delete the files从登录我网站的每个用户的目录中遵循的以下内容:

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

我尝试使用的代码是使用setTimeoutand withunlink但因为max time我们setTimeout可以放置的代码仅approx 24 days此而已。

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

标签: node.jssettimeoutdelete-fileunlink

解决方案


                                          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, m, d, h, mi, 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
                            

推荐阅读