wrench -- 递归文件、文件夹操作一体化解决方案
1.安装
npm install wrench
2.使用
var wrench = require('wrench'),
util = require('util');
3.同步操作
//递归创建目录
wrench.mkdirSyncRecursive(dir, 0777);
//递归删除目录
wrench.rmdirSyncRecursive('my_directory_name', failSilently);
//递归读取目录内容
wrench.readdirSyncRecursive('my_directory_name');
//递归的修改目录的权限
wrench.chmodSyncRecursive('my_directory_name', 0755);
//修改目录的所有者
wrench.chownSyncRecursive("directory", uid, gid);
//拷贝目录
wrench.copyDirSyncRecursive('', '', {
forceDelete: bool, //是否覆盖现有的目录
excludeHiddenUnix: bool, // 是否复制隐藏的Unix文件
preserveFiles: bool,// 如果覆盖的东西和文件已经存在,保持现有的
preserveTimestamps: bool, // Preserve the mtime and atime when copying files
inflateSymlinks: bool, // Whether to follow symlinks or not when copying files
filter: regexpOrFunction,//一个过滤器来匹配文件;如果匹配,什么也不做。
whitelist: bool, // if true every file or directory which doesn't match filter will be ignored
include: regexpOrFunction, //包含过滤器
exclude: regexpOrFunction //排除过滤器
});
//读取文件
var f = new wrench.LineReader('x.txt');
while(f.hasNextLine()) {
util.puts(f.getNextLine());
}
4.异步操作
//递归地读取目录内容
var files = [];
wrench.readdirRecursive('my_directory_name', function(error, curFiles) {
//...
});
wrench.copyDirRecursive(srcDir, newDir, {
forceDelete: bool
}, callbackfn);
网友评论