NodeJs删除非空文件夹的三种方法

fs即filesystem(文件系统)的缩写,是node自带的I/O模块,用来进行本地文件的读写,所有的API都有异步和同步的形式。删除文件的方法为 fs.rmdir(path, callback),他的同步版本为fs.rmdirSync(path),但有个问题是只能删除空文件夹,对于非空文件夹就无能为力了。这里提供了三种方法来实现使用nodejs对一个非空文件夹的删除。可以从 https://github.com/leotian/fs-test 获取demo源代码。

使用fs——递归删除文件后删除文件夹

这个方法依旧使用系统自带的fs模块,实现原理简单粗暴,就是先删除文件夹内所有文件,如果遇到子文件夹再进入子文件夹删除子文件夹内所有文件,直到最后删除所有空文件夹。

让我们来看看fs中我们可以使用的命令:

  1. fs.stat && fs.statSync 提供了访问文件的属性信息
  2. fs.readdir && fs.readdirSync 提供读取文件目录信息
  3. fs.unlink && unlinkSync 进行删除文件操作,不可以删除文件夹
  4. fs.rmdir && fs.rmdirSync 进行删除文件夹操作,但文件夹必须为空文件夹

接下来进行删除,删除需要两步,分别是:

  • 第一步:删除所有文件,清空文件夹(包括子文件夹)
  • 第二步:删除所有空文件夹

代码实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"use strict"

const fs = require('fs')

const filePath = `${__dirname}/aaa`//要删除的文件夹 url

//删除所有的文件(将所有文件夹置空)
function emptyDir(filePath) {
const files = fs.readdirSync(filePath)//读取该文件夹
files.forEach((file) => {
const nextFilePath = `${filePath}/${file}`
const states = fs.statSync(nextFilePath)
if (states.isDirectory()) {
emptyDir(nextFilePath)
} else {
fs.unlinkSync(nextFilePath)
console.log(`删除文件 ${nextFilePath} 成功`)
}
})
}

//删除所有的空文件夹
function rmEmptyDir(filePath) {
const files = fs.readdirSync(filePath)
if (files.length === 0) {
fs.rmdirSync(filePath)
console.log(`删除空文件夹 ${filePath} 成功`)
} else {
let tempFiles = 0
files.forEach((file) => {
tempFiles++
const nextFilePath = `${filePath}/${file}`
rmEmptyDir(nextFilePath)
})
//删除母文件夹下的所有字空文件夹后,将母文件夹也删除
if(tempFiles === files.length) {
fs.rmdirSync(filePath)
console.log(`删除空文件夹 ${filePath} 成功`)
}
}
}

emptyDir(filePath)
rmEmptyDir(filePath)

代码优化:清空文件夹和删除文件夹一次进行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function deleteFolder(filePath) {
const files = []
if (fs.existsSync(filePath)) {
const files = fs.readdirSync(filePath)
files.forEach((file) => {
const nextFilePath = `${filePath}/${file}`
const states = fs.statSync(nextFilePath)
if (states.isDirectory()) {
//recurse
deleteFolder(nextFilePath)
} else {
//delete file
fs.unlinkSync(nextFilePath)
}
})
fs.rmdirSync(filePath)
}
}

deleteFolder(filePath)

使用子进程——执行”rm -rf命令”

我们都知道 Node.js 是以单线程的模式运行的,但它使用的是事件驱动来处理并发,这样有助于我们在多核 cpu 的系统上创建多个子进程,从而提高性能。

每个子进程总是带有三个流对象:child.stdin,child.stdout 和child.stderr。他们可能会共享父进程的 stdio 流,或者也可以是独立的被导流的流对象。

Node 提供了 child_process 模块来创建子进程,方法有:

  • exec - child_process.exec 使用子进程执行命令,缓存子进程的输出,并将子进程的输出以回调函数参数的形式返回。
  • spawn - child_process.spawn 使用指定的命令行参数创建新进程。
  • fork - child_process.fork 是 spawn()的特殊形式,用于在子进程中运行的模块,如 fork(‘./son.js’) 相当于 spawn(‘node’, [‘./son.js’]) 。与spawn方法不同的是,fork会在父进程与子进程之间,建立一个通信管道,用于进程之间的通信。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const fs = require('fs')
const child_process = require('child_process');
const filePath = `${__dirname}/aaa`//要删除的文件夹 url

const workerProcess = child_process.exec(`rm -rf ${filePath}`,
(error, stdout, stderr) => {
if (error) {
console.log(error.stack)
console.log('Error code: '+error.code)
console.log('Signal received: '+error.signal)
}
console.log('stdout: ' + stdout)
console.log('stderr: ' + stderr)
})

workerProcess.on('exit', function (code) {
console.log('子进程已退出,退出码 '+code);
})

使用第三方工具包rimraf

最简单的方法就是安装第三方工具包——rimraf,rimraf 包的作用:以包的形式包装rm -rf命令,就是用来删除文件和文件夹的,不管文件夹是否为空,都可以删除。

  1. npm地址:https://www.npmjs.com/package/rimraf
  2. GitHub地址:https://github.com/isaacs/rimraf

该包只提供一个方法,就是删除文件/文件夹:

1
2
3
4
const rimraf = require('rimraf');
rimraf('./aaa', function (err) { // 删除当前目录下的 aaa
if (err) console.log(err);
});

看了下rimraf的源码,基本也是使用fs递归删除,之后会考虑写一些nodejs包的源码解读。

参考链接

nodejs 文件系统(fs) 删除文件夹及子文件夹下的所有内容

Node.js 多进程