如何将文件a移动到Node.js中的其他分区或设备? [英] How do I move file a to a different partition or device in Node.js?

查看:118
本文介绍了如何将文件a移动到Node.js中的其他分区或设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Node.js脚本中将文件从一个分区移动到另一个分区。当我使用 fs.renameSync 我收到错误:EXDEV,跨设备链接。我将其复制并删除原始文件,但我也没有看到复制文件的命令。如何做到这一点?

I'm trying to move a file from one partition to another in a Node.js script. When I used fs.renameSync I received Error: EXDEV, Cross-device link. I'd copy it over and delete the original, but I don't see a command to copy files either. How can this be done?

推荐答案

在跨不同分区移动文件时需要复制和取消链接。试试这个,

You need to copy and unlink when moving files across different partitions. Try this,

var fs = require('fs');
//var util = require('util');

var is = fs.createReadStream('source_file');
var os = fs.createWriteStream('destination_file');

is.pipe(os);
is.on('end',function() {
    fs.unlinkSync('source_file');
});

/* node.js 0.6 and earlier you can use util.pump:
util.pump(is, os, function() {
    fs.unlinkSync('source_file');
});
*/

这篇关于如何将文件a移动到Node.js中的其他分区或设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆