Delete several files in node.js

后端 未结 8 1409
自闭症患者
自闭症患者 2021-02-15 23:58

What is the best way to delete several files in node.js?

function deleteFiles(files, callback){
    ...
}

var files = [\'file1.js\', \'file2.jpg\', \'file3.css\         


        
8条回答
  •  梦毁少年i
    2021-02-16 00:18

    This deletes the files in an array and runs the callback once only after all files have been deleted.

    function deleteFiles(files, callback){
       if (files.length==0) callback();
       else {
          var f = files.pop();
          fs.unlink(f, function(err){
             if (err) callback(err);
             else {
                console.log(f + ' deleted.');
                deleteFiles(files, callback);
             }
          });
       }
    }
    

提交回复
热议问题