nodejs列出或删除所有文件和目录asynchronous只传递开始path

我一直在通过stackoverflow的主题来find有用的东西,什么都没有。 我需要的是(可能)一些模块,你可以这样调用:

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 }); 

我需要上述function来为我的学生创build迷你Web构build的ftp /代码编辑器。

列表中包含正确的不仅包含文件的结构,而且还包含子目录,这一点非常重要。它不一定非常像我想要的例子那样简单,最重要的是function就在那里。 谢谢你的所有推荐。

我为自己的需要制定了一个模块,可以帮助你。 看看alinex-fs 。 这是node.js fs模块的扩展,可以用作replace。

此外,它有一个非常强大的fs.find()方法,它将recursionsearch和匹配文件,如linux find命令。 要search的内容是通过一个简单的configuration哈希来完成的。 然后你可以遍历结果并删除所有东西(也是recursion的)。

示例用法可能如下所示:

 # 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'); }); }); 

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

我希望这会帮助你更进一步。 如果没有,请让你的问题更具体。