Home > Web Front-end > JS Tutorial > body text

Node.JS loops to delete all files in non-empty folders and subdirectories

亚连
Release: 2018-05-31 10:23:10
Original
2389 people have browsed it

This article mainly introduces the example code of Node.JS cyclically deleting non-empty folders and all files in subdirectories and node.js recursively deleting non-empty folders. Friends who need it can refer to it

Recently, I want to implement a function to cycle folders. The folder may not be empty, but may also contain subfolders and files. I have found some existing libraries on the Internet, but none of them provide deletion progress. So I wrote one myself.

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

var remove = function(tarPath, cb, removeStates) {
 if (!removeStates) {
  removeStates = { cur: 0, all: 0 }
 } else {
  if (!removeStates.cur) {
   removeStates.cur = 0;
  }
  if (!removeStates.all) {
   removeStates.all = 0;
  }
 }
 removeStates.all++;
 fs.stat(tarPath, function(err, states) {
  removeStates.cur++;
  if (err) {
   cb && cb()
   return
  }
  if (states.isDirectory()) {
   fs.readdir(tarPath, function(err, files) {
    if (err) {
     console.log(err)
     cb && cb()
     return
    }
    if (files.length < 1) {
     fs.rmdir(tarPath, cb)
     return
    }
    var count  = 0
    var checkEnd = function() {
     if (++count == files.length) {
      fs.rmdir(tarPath, cb)
     }
    }
    files.forEach(function(file) {
     remove(path.join(tarPath, file), checkEnd, removeStates)
    })
   })
  } else {
   fs.unlink(tarPath, function(err) {
    if (err) {
     console.log(err)
    }
    cb && cb()
    return
   })
  }
 })
 return removeStates
}
Copy after login

It’s very simple to use

var states = remove(&#39;./testdir&#39;, function() {
  console.log(&#39;delete complete&#39;)
  clearInterval(checkTimer)
});
var checkTimer = setInterval(function() {
  console.log(states)
}, 500);
Copy after login

Output result:

node remove.js
{ cur: 546, all: 546 }
delete complete
Copy after login

PS: Let’s look at NodeJs recursively deleting non-empty folders

This article is used for the first time fs.unlink() reports the error "Error: EPERM: operation not permitted, unlink" when deleting a folder. This is because fs.unlink() can only delete files.

fs.rmdir()or fs.rmdirSync()User deletes empty folders, fs.unlink() or fs.unlinkSync() is used to delete files , so deleting non-empty folders requires recursion.

function deleteFolderRecursive(path) {
  if( fs.existsSync(path) ) {
    fs.readdirSync(path).forEach(function(file) {
      var curPath = path + "/" + file;
      if(fs.statSync(curPath).isDirectory()) { // recurse
        deleteFolderRecursive(curPath);
      } else { // delete file
        fs.unlinkSync(curPath);
      }
    });
    fs.rmdirSync(path);
  }
};
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Vue loading custom js file method

Instance of executing function after leaving the vue page

Usage of vue carousel plug-in vue-concise-slider


The above is the detailed content of Node.JS loops to delete all files in non-empty folders and subdirectories. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template