nodejs 以异步方式列出或删除所有文件和目录,仅传递起始路径

nodejs list or remove all files and directories asynchronously passing only start path

我一直在浏览 Whosebug 主题以找到任何有用的东西,但实际上什么也没有。我需要的是(可能)一些模块,你可以这样调用它:

someModule('/start/path/', 'list', function(err, list) {
    // list contains properly structured object of all subdirectories and files
});

还有这个

someModule('/start/path/', 'remove', function(err, doneFlag) {
    // doneFlag contains something like true so i can run callback
});

我需要以上功能来为我的学生创建迷你网络构建 ftp/code 编辑器。

重要的是,列表不仅包括文件的正确结构,还包括文件所在的子目录的正确结构。它真的不必像我理想的示例中那样简单,最重要的是功能在那里。感谢您的所有推荐。

我根据自己的需要做了一个模块,希望对你有帮助。看看alinex-fs。这是 node.js fs 模块的扩展,可以用作替换。

此外,它还有一个非常强大的 fs.find() 方法,可以像 linux find 命令一样递归搜索和匹配文件。要搜索的内容是通过一个简单的配置哈希来完成的。 然后你可以遍历结果并删除所有内容(也是递归的)。

使用示例可能如下所示:

# include the module
var fs = require('alinex-fs');

# search asynchronouse
fs.find('/tmp/some/directory', { 
  include: 'test*',
  type: 'dir'
  modifiedBefore: 'yesterday 12:00'
  # and much more possibilities...
}, function(err, list) {
  if (err) return console.error(err);

  # async included here for readability but mostly moved to top
  var async = require('async');
  # parallel loop over list
  async.each(list, function(file, cb) {
    # remove file or dir
    return fs.remove(file, cb);
  }, function(err) {
    if (err) return console.log(err);
    console.log('done');
  });

});

如果您已经有了需要删除的条目列表,您也可以只使用上面代码的内部函数。

我希望这能帮助你更进一步。如果不是,请让您的问题更具体。