纯异步nodejs文件夹(目录)复制
思路:
1、callback 驱动
2、递归所有需要复制文件
3、在一定阀值下并发复制文件
4、运行需要安装 async.js npm install async
代码如下:
- var async = require("async");
- var fs = require("fs");
- var path = require("path");
- // cursively make dir
- function mkdirs(p, mode, f, made) {
- if (typeof mode === 'function' || mode === undefined) {
- f = mode;
- mode = 0777 & (~process.umask());
- }
- if (!made)
- made = null;
- var cb = f || function () {};
- if (typeof mode === 'string')
- mode = parseInt(mode, 8);
- p = path.resolve(p);
- fs.mkdir(p, mode, function (er) {
- if (!er) {
- made = made || p;
- return cb(null, made);
- }
- switch (er.code) {
- case 'ENOENT':
- mkdirs(path.dirname(p), mode, function (er, made) {
- if (er) {
- cb(er, made);
- } else {
- mkdirs(p, mode, cb, made);
- }
- });
- break;
- // In the case of any other error, just see if there's a dir
- // there already. If so, then hooray! If not, then something
- // is borked.
- default:
- fs.stat(p, function (er2, stat) {
- // if the stat fails, then that's super weird.
- // let the original error be the failure reason.
- if (er2 || !stat.isDirectory()) {
- cb(er, made);
- } else {
- cb(null, made)
- };
- });
- break;
- }
- });
- }
- // single file copy
- function copyFile(file, toDir, cb) {
- async.waterfall([
- function (callback) {
- fs.exists(toDir, function (exists) {
- if (exists) {
- callback(null, false);
- } else {
- callback(null, true);
- }
- });
- }, function (need, callback) {
- if (need) {
- mkdirs(path.dirname(toDir), callback);
- } else {
- callback(null, true);
- }
- }, function (p, callback) {
- var reads = fs.createReadStream(file);
- var writes = fs.createWriteStream(path.join(path.dirname(toDir), path.basename(file)));
- reads.pipe(writes);
- //don't forget close the when all the data are read
- reads.on("end", function () {
- writes.end();
- callback(null);
- });
- reads.on("error", function (err) {
- console.log("error occur in reads");
- callback(true, err);
- });
- }
- ], cb);
- }
- // cursively count the files that need to be copied
- function _ccoutTask(from, to, cbw) {
- async.waterfall([
- function (callback) {
- fs.stat(from, callback);
- },
- function (stats, callback) {
- if (stats.isFile()) {
- cbw.addFile(from, to);
- callback(null, []);
- } else if (stats.isDirectory()) {
- fs.readdir(from, callback);
- }
- },
- function (files, callback) {
- if (files.length) {
- for (var i = 0; i < files.length; i++) {
- _ccoutTask(path.join(from, files[i]), path.join(to, files[i]), cbw.increase());
- }
- }
- callback(null);
- }
- ], cbw);
- }
- // wrap the callback before counting
- function ccoutTask(from, to, cb) {
- var files = [];
- var count = 1;
- function wrapper(err) {
- count--;
- if (err || count <= 0) {
- cb(err, files)
- }
- }
- wrapper.increase = function () {
- count++;
- return wrapper;
- }
- wrapper.addFile = function (file, dir) {
- files.push({
- file : file,
- dir : dir
- });
- }
- _ccoutTask(from, to, wrapper);
- }
- function copyDir(from, to, cb) {
- if(!cb){
- cb=function(){};
- }
- async.waterfall([
- function (callback) {
- fs.exists(from, function (exists) {
- if (exists) {
- callback(null, true);
- } else {
- console.log(from + " not exists");
- callback(true);
- }
- });
- },
- function (exists, callback) {
- fs.stat(from, callback);
- },
- function (stats, callback) {
- if (stats.isFile()) {
- // one file copy
- copyFile(from, to, function (err) {
- if (err) {
- // break the waterfall
- callback(true);
- } else {
- callback(null, []);
- }
- });
- } else if (stats.isDirectory()) {
- ccoutTask(from, to, callback);
- }
- },
- function (files, callback) {
- // prevent reaching to max file open limit
- async.mapLimit(files, 10, function (f, cb) {
- copyFile(f.file, f.dir, cb);
- }, callback);
- }
- ], cb);
- }
- var start = new Date().getTime();
- copyDir("F:\\gear", "F:\\gear2", function (err) {
- if (err) {
- console.log("error ocur");
- console.dir(err);
- } else {
- console.log("copy ok");
- console.log("consume time:" + (new Date().getTime() - start))
- }
- });
纯异步nodejs文件夹(目录)复制的更多相关文章
- 工具:从一个文件夹中复制jar到另一个文件夹中
工具类:从一个文件夹中复制jar到另一个文件夹中 需要的小伙伴可以试一试,很爽哦,有时候真的很需要! 需求:当我们拿到一个maven项目时,而maven项目的jar包都是通过pom.xml文件管理的, ...
- 【转载】ASP.NET工具类:文件夹目录Directory操作工具类
在ASP.NET开发网站的过程中,有时候会涉及到文件夹相关操作,如判断文件夹目录是否存在.删除文件夹目录.创建文件.删除文件.复制文件夹等等.这一批有关文件目录的操作可以通过Directory类.Fi ...
- A1制作文件夹目录
第一步 在文件夹目录下建立bat文件,填写以下内容: dir *.* /B >目录.txt 最后双击bat文件. 第二步 运行后复制目录.txt文件内容到空白excel 使用hyperlink函 ...
- Node.js 文件夹目录结构创建
第一次接触NodeJS的文件系统就被它的异步的响应给搞晕了,后来发现NodeJS判断文件夹是否存在和创建文件夹是还有同步方法的,但是还是想尝试使用异步的方法去实现. 使用的方法:fs.exists(p ...
- Java修炼——文件夹的复制
文件夹的复制用到了俩个流:缓冲流和文件字节流 缓冲流用来加快写入和读取速度. 在这里我简述一下复制文件夹的过程,当然复制文件夹都可以,复制文件更是不在话下 1.首先要明确俩点.要复制的文件夹的位置(源 ...
- linux系统下修改文件夹目录权限
linux系统下修改文件夹目录权限 文件夹权限问题 Linux.Fedora.Ubuntu修改文件.文件夹权限的方法差不多.很多人开始接触Linux时都很头痛Linux的文件权限问题.这里告诉大家如何 ...
- linux下跨服务器文件文件夹的复制
文件的复制:scp –P (端口号) ./authorized_keys berchina@hadoop002:/home/berchina 文件夹的复制:scp -r -P (端口号) /home/ ...
- 【转载】C#递归删除文件夹目录及文件
在C#文件操作过程中,有时候需要删除相应目录,如果文件夹中含有其他文件夹或者文件,也需要一并进行删除,此时可能就需要使用递归来删除文件夹目录以及文件,递归过程中,如果遍历的对象是文件夹,则删除文件夹, ...
- C# -- 使用递归列出文件夹目录及目录下的文件
使用递归列出文件夹目录及目录的下文件 1.使用递归列出文件夹目录及目录下文件,并将文件目录结构在TreeView控件中显示出来. 新建一个WinForm应用程序,放置一个TreeView控件: 代码实 ...
随机推荐
- java中用反射访问私有方法和私有成员[转]
转自: http://zhouyangchenrui.iteye.com/blog/470521 java的反射可以绕过访问权限,访问到类的私有方法和成员.可能这点会引起安全性的讨论.反射的使用帮助解 ...
- WPF图形/文字特别效果之一:交叉效果探讨
原文:WPF图形/文字特别效果之一:交叉效果探讨 为了说明问题,先看下图:图1 完全重叠的单一颜色文字它是2008几个字的叠加,并且颜色为单一的红色.如果不仔细分辨,你或许无法一下子看出是2008. ...
- 创建一个显示所有预定义WPF颜色的ListBox
原文 Creating a ListBox that Shows All Predefined WPF Colors 在WPF中,您可以使用Colors类访问一系列预定义颜色,这些颜色定义为Color ...
- OpenGL(十一) BMP真彩文件的显示和复制操作
glut窗口除了可以绘制矢量图之外,还可以显示BMP文件,用函数glDrawPixels把内存块中的图像数据绘制到窗口上,glDrawPixels函数原型: glDrawPixels (GLsizei ...
- Feature extraction - sklearn文本特征提取
http://blog.csdn.net/pipisorry/article/details/41957763 文本特征提取 词袋(Bag of Words)表征 文本分析是机器学习算法的主要应用领域 ...
- C#调用C/C++ DLL 参数传递和回调函数的总结
原文:C#调用C/C++ DLL 参数传递和回调函数的总结 Int型传入: Dll端: extern "C" __declspec(dllexport) int Add(int a ...
- seajs教程(一):基本用法
介绍 SeaJS 是一个适用于 Web 浏览器端的模块加载器.使用 SeaJS,可以更好地组织 JavaScript 代码. Sea.js 遵循 CMD 规范,模块化JS代码.依赖的自动加载.配置的简 ...
- 深入理解Delphi的消息机制(别人写的,简明扼要,用来复习)
永远记住,无论你是用 SDK 还是借用 VCL 来创建窗口,都要遵循 Windows 的游戏规则,即先注册窗口类,然后再创建窗口实例,在消息循环中写实现代码.你还要知道 Windows 已经为了我们预 ...
- 编解码TIFF图像
解码: // Open a Stream and decode a TIFF image Stream imageStreamSource = new FileStream("tulipfa ...
- WebApi实现验证授权Token,WebApi生成文档等 - CSDN博客
原文:WebApi实现验证授权Token,WebApi生成文档等 - CSDN博客 using System; using System.Linq; using System.Web; using S ...