nodejs文件操作,可以在不同盘符之间复制文件吗?
发布于 7个月前 作者 jintaolee 745 次浏览

比如,我想把C盘下的文件复制到E盘,怎么操作??

3 回复

把C盘文件读出来~写到E盘下~

var fs = require(‘fs’); var util = require(“util”);

var base = base || {};

base.createFloder = function(path, callback) { var arr = path.split(‘/’); var floderPath = '’; var index = 0; (function() { var callFunc = arguments.callee; var self = this; floderPath = floderPath + arr[index] + '/’; fs.readdir(floderPath, function(err, files) { if(err) { fs.mkdir(floderPath, 0777, function(err) { console.log("[create floder] create floder " + floderPath); if (++index < arr.length) { callFunc.call(self); } else { callback? callback() : '’; } }); } else { if (++index < arr.length) { callFunc.call(self); } else { callback? callback() : '’; } } }); })(); };

base.copyFile = function(fileInPath, fileOutPath, callback) { var fileOutFolderPath = fileOutPath.substr(0, fileOutPath.lastIndexOf(‘/’)); base.createFloder(fileOutFolderPath, function() { console.log("[copy] copy " + fileInPath + " to " + fileOutPath); var is = fs.createReadStream(fileInPath); var os = fs.createWriteStream(fileOutPath); readableStream.pipe(is, os, function(err){
if(err) { console.log("[copy] copy err: " + err); return ; } callback? callback() : '’; }); }); };

base.copyFile("c:/test.txt", "d:/test.txt", function() { console.log(“copy success”); });

记得当年NODE在linux下无法在两个分区下移动文件,所以一直使用require(‘child_process’).exec(‘mv xxxxx xxx’)来移动文件。不知道现在的版本有没解决。

回到顶部