如何在node.js上移动文件(如mv命令shell) ?是否有任何方法,或者我应该读取一个文件,写到一个新文件,并删除旧文件?
当前回答
跑龙套。节点0.10弃用Pump,产生警告信息
util.pump() is deprecated. Use readableStream.pipe() instead
因此,使用流复制文件的解决方案是:
var source = fs.createReadStream('/path/to/source');
var dest = fs.createWriteStream('/path/to/dest');
source.pipe(dest);
source.on('end', function() { /* copied */ });
source.on('error', function(err) { /* error */ });
其他回答
如果您试图移动或重命名一个node.js源文件,请尝试https://github.com/viruschidai/node-mv。它将更新所有其他文件中对该文件的引用。
跑龙套。节点0.10弃用Pump,产生警告信息
util.pump() is deprecated. Use readableStream.pipe() instead
因此,使用流复制文件的解决方案是:
var source = fs.createReadStream('/path/to/source');
var dest = fs.createWriteStream('/path/to/dest');
source.pipe(dest);
source.on('end', function() { /* copied */ });
source.on('error', function(err) { /* error */ });
就像上面的回答中所说的那样,我的2分:copy()方法不应该在没有轻微调整的情况下原样用于复制文件:
function copy(callback) {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(newPath);
readStream.on('error', callback);
writeStream.on('error', callback);
// Do not callback() upon "close" event on the readStream
// readStream.on('close', function () {
// Do instead upon "close" on the writeStream
writeStream.on('close', function () {
callback();
});
readStream.pipe(writeStream);
}
复制函数封装在Promise中:
function copy(oldPath, newPath) {
return new Promise((resolve, reject) => {
const readStream = fs.createReadStream(oldPath);
const writeStream = fs.createWriteStream(newPath);
readStream.on('error', err => reject(err));
writeStream.on('error', err => reject(err));
writeStream.on('close', function() {
resolve();
});
readStream.pipe(writeStream);
})
但是,请记住,如果目标文件夹不存在,文件系统可能会崩溃。
这是对teoman shipahi的回答的重新讨论,使用了一个稍微不那么模糊的名称,并遵循了在尝试调用代码之前定义代码的设计原则。(虽然node允许您做其他事情,但本末倒置并不是一个好的实践。)
function rename_or_copy_and_delete (oldPath, newPath, callback) {
function copy_and_delete () {
var readStream = fs.createReadStream(oldPath);
var writeStream = fs.createWriteStream(newPath);
readStream.on('error', callback);
writeStream.on('error', callback);
readStream.on('close',
function () {
fs.unlink(oldPath, callback);
}
);
readStream.pipe(writeStream);
}
fs.rename(oldPath, newPath,
function (err) {
if (err) {
if (err.code === 'EXDEV') {
copy_and_delete();
} else {
callback(err);
}
return;// << both cases (err/copy_and_delete)
}
callback();
}
);
}
在下面URL的帮助下,您可以复制或移动您的文件CURRENT Source到Destination Source
https://coursesweb.net/nodejs/move-copy-file
/*********Moves the $file to $dir2 Start *********/ var moveFile = (file, dir2)=>{ //include the fs, path modules var fs = require('fs'); var path = require('path'); //gets file name and adds it to dir2 var f = path.basename(file); var dest = path.resolve(dir2, f); fs.rename(file, dest, (err)=>{ if(err) throw err; else console.log('Successfully moved'); }); }; //move file1.htm from 'test/' to 'test/dir_1/' moveFile('./test/file1.htm', './test/dir_1/'); /*********Moves the $file to $dir2 END *********/ /*********copy the $file to $dir2 Start *********/ var copyFile = (file, dir2)=>{ //include the fs, path modules var fs = require('fs'); var path = require('path'); //gets file name and adds it to dir2 var f = path.basename(file); var source = fs.createReadStream(file); var dest = fs.createWriteStream(path.resolve(dir2, f)); source.pipe(dest); source.on('end', function() { console.log('Succesfully copied'); }); source.on('error', function(err) { console.log(err); }); }; //example, copy file1.htm from 'test/dir_1/' to 'test/' copyFile('./test/dir_1/file1.htm', './test/'); /*********copy the $file to $dir2 END *********/
推荐文章
- 在MacOS X上推荐用什么方式安装Node.js、nvm和npm ?
- 如何将Node.js流的内容读入字符串变量?
- 如何在node.js中移动文件?
- 如何修改文本文件?
- npm install和npm run build有什么区别?
- 使用async/await尝试/捕获块
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- 有没有办法修复包锁。json lockfileVersion所以npm使用特定的格式?
- 如何使用npm全局安装一个模块?
- Python列表目录,子目录和文件
- 实时http流到HTML5视频客户端的最佳方法
- 使用node.js下载图像
- Node.js Express中的HTTP GET请求
- Node.js:将文本文件读入数组。(每一行都是数组中的一项。)
- 我如何知道我在S3桶中存储了多少对象?