nodejs删除文件夹的方法有哪些?

2026-03-31 13:230阅读0评论SEO资源
  • 内容介绍
  • 相关推荐

本文共计457个文字,预计阅读时间需要2分钟。

nodejs删除文件夹的方法有哪些?

Node.js 删除文件夹的方法:

1. 创建一个js示例文件;

2.引入fs模块;

3.通过`function deleteall(path) { ... }`方法删除文件夹即可。

示例代码:

javascript

const fs=require('fs');

function deleteall(path) { let files=[]; // 读取文件夹内容 fs.readdirSync(path).forEach(function(file) { files.push(file); });

// 遍历文件夹 files.forEach(function(file) { let curPath=path + / + file; // 如果是文件夹,递归调用 if (fs.statSync(curPath).isDirectory()) { deleteall(curPath); } else { // 如果是文件,删除文件 fs.unlinkSync(curPath); } }); // 删除文件夹 fs.rmdirSync(path);}

// 使用示例deleteall('path/to/directory');

环境操作:

在Windows 7系统、Node.js 10.16.2版本、DELL G3电脑上,你可以直接运行上述示例代码。

阅读全文

本文共计457个文字,预计阅读时间需要2分钟。

nodejs删除文件夹的方法有哪些?

Node.js 删除文件夹的方法:

1. 创建一个js示例文件;

2.引入fs模块;

3.通过`function deleteall(path) { ... }`方法删除文件夹即可。

示例代码:

javascript

const fs=require('fs');

function deleteall(path) { let files=[]; // 读取文件夹内容 fs.readdirSync(path).forEach(function(file) { files.push(file); });

// 遍历文件夹 files.forEach(function(file) { let curPath=path + / + file; // 如果是文件夹,递归调用 if (fs.statSync(curPath).isDirectory()) { deleteall(curPath); } else { // 如果是文件,删除文件 fs.unlinkSync(curPath); } }); // 删除文件夹 fs.rmdirSync(path);}

// 使用示例deleteall('path/to/directory');

环境操作:

在Windows 7系统、Node.js 10.16.2版本、DELL G3电脑上,你可以直接运行上述示例代码。

阅读全文