Javascript 如何从目录中删除所有文件而不删除 Node.js 中的目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27072866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove all files from directory without removing directory in Node.js
提问by youbetternot
How to remove all files from a directory without removing a directory itself using Node.js?
I want to remove temporary files. I am not any good with filesystems yet.
如何使用 Node.js 从目录中删除所有文件而不删除目录本身?
我想删除临时文件。我还不太擅长文件系统。
I have found thismethod, which will remove files and the directory. In that, something like /path/to/directory/*won't work.
我找到了这个方法,它将删除文件和目录。在那种情况下,类似的东西是/path/to/directory/*行不通的。
I don't really know what commands should use. Thanks for the help.
我真的不知道应该使用什么命令。谢谢您的帮助。
回答by Rodrigo5244
To remove all files from a directory, first you need to list all files in the directory using fs.readdir, then you can use fs.unlinkto remove each file. Also fs.readdirwill give just the file names, you need to concat with the directory name to get the full path.
要从目录中删除所有文件,首先需要使用 列出目录中的所有文件fs.readdir,然后可以使用fs.unlink删除每个文件。也fs.readdir将只给出文件名,您需要与目录名连接以获取完整路径。
Here is an example
这是一个例子
const fs = require('fs');
const path = require('path');
const directory = 'test';
fs.readdir(directory, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(directory, file), err => {
if (err) throw err;
});
}
});
回答by Nicolas Guérinet
There is package called rimraf that is very handy. It is the UNIX command rm -rf for node.
有一个名为 rimraf 的软件包,非常方便。它是用于节点的 UNIX 命令 rm -rf。
Nevertheless, it can be too powerful too because you can delete folders very easily using it. The following commands will delete the files inside the folder. If you remove the *, you will remove the log folder.
尽管如此,它也可能过于强大,因为您可以使用它非常轻松地删除文件夹。以下命令将删除文件夹内的文件。如果删除 *,您将删除日志文件夹。
const rimraf = require('rimraf');
rimraf('./log/*', function () { console.log('done'); });
回答by Samiul Alam
Yes, there is a module fs-extra. There is a method .emptyDir()inside this module which does the job. Here is an example:
是的,有一个模块fs-extra。.emptyDir()这个模块中有一个方法可以完成这项工作。下面是一个例子:
const fsExtra = require('fs-extra')
fsExtra.emptyDirSync(fileDir)
There is also an asynchronous version of this module too. Anyone can check out the link.
这个模块也有一个异步版本。任何人都可以查看链接。
回答by Jason Kim
Building on @Waterscroll's response, if you want to use async and await in node 8+:
基于@Waterscroll 的响应,如果您想在节点 8+ 中使用 async 和 await:
const fs = require('fs');
const util = require('util');
const readdir = util.promisify(fs.readdir);
const unlink = util.promisify(fs.unlink);
const directory = 'test';
async function toRun() {
try {
const files = await readdir(directory);
const unlinkPromises = files.map(filename => unlink(`${directory}/${filename}`));
return Promise.all(unlinkPromises);
} catch(err) {
console.log(err);
}
}
toRun();
回答by Nick
(2020) Building on Jason Kim and Shikyo's responses: There's an experimental fsPromises.rmdir()method now, which will recursively delete files and folders including the one you pass it.
(2020) 基于 Jason Kim 和 Shikyo 的回答:fsPromises.rmdir()现在有一种实验方法,可以递归删除文件和文件夹,包括您传递的文件和文件夹。
const fsPromises = require('fs').promises
const directory = 'your/directory/path/here'
await fsPromises.rmdir(directory, {
recursive: true
})
More info: https://nodejs.org/api/fs.html#fs_fspromises_rmdir_path_options
更多信息:https: //nodejs.org/api/fs.html#fs_fspromises_rmdir_path_options
回答by Brett Anderson
You can have a script that does rm /dist**and that will remove every single file inside of the dist folder and keep the dist folder itself untouched
你可以有一个脚本,它rm /dist**会删除 dist 文件夹中的每个文件,并保持 dist 文件夹本身不变

