删除(取消链接)与正则表达式匹配的文件

时间:2021-09-02 23:19:08

I want to delete several files from a directory, matching a regex. Something like this:

我想删除目录中的几个文件,匹配正则表达式。像这样的东西:

// WARNING: not real code
require('fs').unlink(/script\.\d+\.js$/);

Since unlink doesn't support regexes, I'm using this instead:

由于unlink不支持正则表达式,我使用的是:

var fs = require('fs');

fs.readdir('.', (error, files) => {
    if (error) throw error;

    files.filter(name => /script\.\d+\.js$/.test(name)).forEach(fs.unlink);
});

which works, but IMO is a little more complex than it should be.

哪个有效,但IMO比它应该复杂一点。


Is there a better built-in way to delete files that match a regex (or even just use wildcards)?

是否有更好的内置方法来删除与正则表达式匹配的文件(甚至只是使用通配符)?

3 个解决方案

#1


9  

You can look into glob https://npmjs.org/package/glob

你可以查看glob https://npmjs.org/package/glob

require("glob").glob("*.txt", function (er, files) { ... });
//or
files = require("glob").globSync("*.txt");

glob internally uses minimatch. It works by converting glob expressions into JavaScript RegExp objects. https://github.com/isaacs/minimatch

glob内部使用minimatch。它的工作原理是将glob表达式转换为JavaScript RegExp对象。 https://github.com/isaacs/minimatch

You can do whatever you want with the matched files in the callback (or in case of globSync the returned object).

您可以使用回调中的匹配文件执行任何操作(或者在globSync的情况下返回对象)。

#2


1  

I have a very simple solution to do this. Read the directory in node.js using fs.readdir API. This will give an array of all the files in the directory. Once you have that array, iterate over it using for loop, apply regex. The below code will delete all files starting with "en" and extension ".js"

我有一个非常简单的解决方案来做到这一点。使用fs.readdir API读取node.js中的目录。这将给出目录中所有文件的数组。一旦你有了这个数组,使用for循环迭代它,应用正则表达式。以下代码将删除所有以“en”和扩展名“.js”开头的文件

fs.readdir('.', (err, files)=>{
   for (var i = 0, len = files.length; i < len; i++) {
      var match = files[i].match(/en.*.js/);
      if(match !== null)
          fs.unlink(match[0]);
   }
});

#3


0  

The answer could depend on your environment. It looks like you are running on node.js. A quick perusal of the node.js documentation suggests there is no "built in" way to do this, i.e., there isn't a single function call that will do this for you. The next best thing might involve a small number of function calls. As I wrote in my comment, I don't think there's any easy way to make your suggested answer much briefer just relying on the standard node.js function calls. That is, if I were in your shoes, I would go with the solution you already suggested (though slightly cleaned up).

答案可能取决于您的环境。看起来你在node.js上运行。仔细阅读node.js文档表明没有“内置”方法来执行此操作,即没有一个函数调用可以为您执行此操作。下一个最好的事情可能涉及少量函数调用。正如我在评论中写的那样,我认为没有任何简单的方法可以让你的建议答案更简单,只需依靠标准的node.js函数调用。也就是说,如果我在你的鞋子里,我会选择你已经建议的解决方案(虽然稍微清理过)。

One alternative is to go to the shell, e.g.,

另一种方法是转到shell,例如,

var exec = require('child_process').exec;
exec('ls | grep "script[[:digit:]]\\\+.js" | xargs rm');

Personally, I would strongly prefer your offered solution over this gobbledygook, but maybe you're shooting for something different.

就个人而言,我非常希望你提供的解决方案超过这个gobbledygook,但也许你正在拍摄不同的东西。

#1


9  

You can look into glob https://npmjs.org/package/glob

你可以查看glob https://npmjs.org/package/glob

require("glob").glob("*.txt", function (er, files) { ... });
//or
files = require("glob").globSync("*.txt");

glob internally uses minimatch. It works by converting glob expressions into JavaScript RegExp objects. https://github.com/isaacs/minimatch

glob内部使用minimatch。它的工作原理是将glob表达式转换为JavaScript RegExp对象。 https://github.com/isaacs/minimatch

You can do whatever you want with the matched files in the callback (or in case of globSync the returned object).

您可以使用回调中的匹配文件执行任何操作(或者在globSync的情况下返回对象)。

#2


1  

I have a very simple solution to do this. Read the directory in node.js using fs.readdir API. This will give an array of all the files in the directory. Once you have that array, iterate over it using for loop, apply regex. The below code will delete all files starting with "en" and extension ".js"

我有一个非常简单的解决方案来做到这一点。使用fs.readdir API读取node.js中的目录。这将给出目录中所有文件的数组。一旦你有了这个数组,使用for循环迭代它,应用正则表达式。以下代码将删除所有以“en”和扩展名“.js”开头的文件

fs.readdir('.', (err, files)=>{
   for (var i = 0, len = files.length; i < len; i++) {
      var match = files[i].match(/en.*.js/);
      if(match !== null)
          fs.unlink(match[0]);
   }
});

#3


0  

The answer could depend on your environment. It looks like you are running on node.js. A quick perusal of the node.js documentation suggests there is no "built in" way to do this, i.e., there isn't a single function call that will do this for you. The next best thing might involve a small number of function calls. As I wrote in my comment, I don't think there's any easy way to make your suggested answer much briefer just relying on the standard node.js function calls. That is, if I were in your shoes, I would go with the solution you already suggested (though slightly cleaned up).

答案可能取决于您的环境。看起来你在node.js上运行。仔细阅读node.js文档表明没有“内置”方法来执行此操作,即没有一个函数调用可以为您执行此操作。下一个最好的事情可能涉及少量函数调用。正如我在评论中写的那样,我认为没有任何简单的方法可以让你的建议答案更简单,只需依靠标准的node.js函数调用。也就是说,如果我在你的鞋子里,我会选择你已经建议的解决方案(虽然稍微清理过)。

One alternative is to go to the shell, e.g.,

另一种方法是转到shell,例如,

var exec = require('child_process').exec;
exec('ls | grep "script[[:digit:]]\\\+.js" | xargs rm');

Personally, I would strongly prefer your offered solution over this gobbledygook, but maybe you're shooting for something different.

就个人而言,我非常希望你提供的解决方案超过这个gobbledygook,但也许你正在拍摄不同的东西。