简体   繁体   中英

node js - fs.unlink only deletes first file in a forEach sequence then throw ENOENT no such file or directory

I am trying to write a function that will remove files in a directory that are more than 1 day old. Currently, my logic is as follows:

  • read all files in directory and put them in an array
  • for each file check if the difference between current ctime and the file's ctime is greater than 1 day
  • if yes, then delete it
  • check next file in the array

My code to accomplish the above is below:

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
});

So far, my code has been successful only in removing the last file in the array someotherfile.png . Then it starts complaining like this:

/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' }

Could someone please tell me what is wrong?

It turns out I missed declaring my filePath as a variable. Thanks to @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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM