node js - fs.unlink只删除forEach序列中的第一个文件,然后抛出ENOENT没有这样的文件或目录

时间:2018-06-06 09:43:59

标签: javascript node.js fs

我正在尝试编写一个函数来删除目录中超过1天的文件。目前,我的逻辑如下:

  • 读取目录中的所有文件并将它们放入数组
  • 为每个文件检查当前ctime与文件ctime之间的差异是否大于1天
  • 如果是,则删除它
  • 检查数组中的下一个文件

我完成上述任务的代码如下:

var fs = require('fs');
var path = require('path');

var uploadsDir = __dirname + '/uploaded_files'

fs.readdir(uploadsDir, function(err, files) {
    var removedFiles = [];
    files.forEach(function(file, index) {
        filePath = path.join(uploadsDir, file);
        console.log(filePath);

        fs.stat(filePath, function(err, stat) {
            if (err) {
            return console.error(err);
            }

            var now = new Date();
            var endTime = new Date(stat.ctime);
            var diffDays = parseInt((now - endTime) / (1000 * 60 * 60 * 24))
            console.log(now);
            console.log(endTime);
            console.log(diffDays + ' days');

            if (diffDays >= 1) {
                fs.unlink(filePath, function(err) {
                    if (err) {
                        return console.log(err);
                    }
                });
                removedFiles.push(file)
                console.log(filePath + ' has been removed!');
                console.log(file);
                console.log('\n');
            }
        }); // end of fs.stat
    }); // end of forEach
});

到目前为止,我的代码仅在删除数组someotherfile.png中的 last 文件时才成功。然后它开始抱怨这样:

/Users/username/api/uploaded_files/.DS_Store
/Users/username/api/uploaded_files/anotherfile.png
/Users/username/api/uploaded_files/somefile.png
/Users/username/api/uploaded_files/someotherfile.png
2018-06-06T09:21:30.010Z
2018-06-04T21:50:33.546Z
1 days
/Users/username/api/uploaded_files/someotherfile.png.png has been removed!
.DS_Store

2018-06-06T09:21:30.012Z
2018-06-04T21:50:46.509Z
1 days
/Users/username/api/uploaded_files/someotherfile.png has been removed!
anotherfile.png

2018-06-06T09:21:30.012Z
2018-06-04T21:50:46.508Z
1 days
/Users/username/api/uploaded_files/someotherfile.png has been removed!
somefile.png

{ Error: ENOENT: no such file or directory, unlink '/Users/username/api/uploaded_files/somefile.png'
  errno: -2,
  code: 'ENOENT',
  syscall: 'unlink',
  path: '/Users/username/api/uploaded_files/somefile.png' }

{ Error: ENOENT: no such file or directory, unlink '/Users/username/api/uploaded_files/somefile.png'
  errno: -2,
  code: 'ENOENT',
  syscall: 'unlink',
  path: '/Users/username/api/uploaded_files/somefile.png' }
{ Error: ENOENT: no such file or directory, unlink '/Users/username/api/uploaded_files/somefile.png'
  errno: -2,
  code: 'ENOENT',
  syscall: 'unlink',
  path: '/Users/username/api/uploaded_files/somefile.png' }

有人可以告诉我有什么问题吗?

1 个答案:

答案 0 :(得分:0)

事实证明我错过了将filePath声明为变量。感谢@CertainPerformance。

var fs = require('fs');
var path = require('path');

var uploadsDir = __dirname + '/uploaded_files'

fs.readdir(uploadsDir, function(err, files) {
    var removedFiles = [];
    files.forEach(function(file, index) {
        var filePath = path.join(uploadsDir, file); //added this var here and it works now!
        console.log(filePath);