Node.JS 循环递归复制文件夹目录及其子文件夹下的
网络编程 2021-07-04 17:32www.168986.cn编程入门
在Node.js中,要实现目录文件夹的循环递归复制也非常简单,使用fs模块即可,仅需几行,而且性能也不错,我们先来实现文件的复制,需要的朋友可以参考下
实现代码一:
var fs = require('fs') var path = require('path') var copyFile = function(srcPath, tarPath, cb) { var rs = fs.createReadStream(srcPath) rs.on('error', function(err) { if (err) { console.log('read error', srcPath) } cb && cb(err) }) var ws = fs.createWriteStream(tarPath) ws.on('error', function(err) { if (err) { console.log('write error', tarPath) } cb && cb(err) }) ws.on('close', function(ex) { cb && cb(ex) }) rs.pipe(ws) }
复制目录及其子目录
var copyFolder = function(srcDir, tarDir, cb) { fs.readdir(srcDir, function(err, files) { var count = 0 var checkEnd = function() { ++count == files.length && cb && cb() } if (err) { checkEnd() return } files.forEach(function(file) { var srcPath = path.join(srcDir, file) var tarPath = path.join(tarDir, file) fs.stat(srcPath, function(err, stats) { if (stats.isDirectory()) { console.log('mkdir', tarPath) fs.mkdir(tarPath, function(err) { if (err) { console.log(err) return } copyFolder(srcPath, tarPath, checkEnd) }) } else { copyFile(srcPath, tarPath, checkEnd) } }) }) //为空时直接回调 files.length === 0 && cb && cb() }) }
使用时
copyFolder('...', '....', function(err) { if (err) { return } //continue })
文章到此结束,希望有帮助的朋友多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程