javascript 如何将文件 a 移动到 Node.js 中的不同分区或设备?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4568689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:07:57  来源:igfitidea点击:

How do I move file a to a different partition or device in Node.js?

javascriptnode.jsrename

提问by Facebook Staff are Complicit

I'm trying to move a file from one partition to another in a Node.js script. When I used fs.renameSyncI 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?

我正在尝试在 Node.js 脚本中将文件从一个分区移动到另一个分区。当我使用时,fs.renameSync我收到了Error: EXDEV, Cross-device link. 我会复制它并删除原始文件,但我也没有看到复制文件的命令。如何才能做到这一点?

回答by Chandra Sekar

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');
});
*/

回答by erapert

One more solution to the problem.

问题的另一种解决方案。

There's a package called fs.extrawritten by "coolaj86" on npm.

npm 上有一个由“coolaj86”编写的名为fs.extra的包。

You use it like so: npm install fs.extra

你像这样使用它: npm install fs.extra

fs = require ('fs.extra');
fs.move ('foo.txt', 'bar.txt', function (err) {
    if (err) { throw err; }
    console.log ("Moved 'foo.txt' to 'bar.txt'");
});

I've read the source code for this thing. It attempts to do a standard fs.rename()then, if it fails, it does a copy and deletes the original using the same util.pump()that @chandru uses.

我已经阅读了这件事的源代码。它尝试执行一个标准fs.rename(),如果失败,它会复制并使用util.pump()@chandru 使用的相同内容删除原始文件。

回答by Justin

I know this is already answered, but I ran across a similar problem and ended up with something along the lines of:

我知道这已经得到了回答,但我遇到了类似的问题并最终得到了以下内容:

require('child_process').spawn('cp', ['-r', source, destination])

What this does is call the command cp("copy"). Since we're stepping outside of Node.js, this command needs to be supported by your system.

这样做是调用命令cp(“复制”)。由于我们是在 Node.js 之外进行的,因此您的系统需要支持此命令。

I know it's not the most elegant, but it did what I needed :)

我知道它不是最优雅的,但它满足了我的需求:)

回答by ehrhardt

to import the module and save it to your package.json file

导入模块并将其保存到您的 package.json 文件中

npm install mv --save

then use it like so:

然后像这样使用它:

var mv = require('mv');

mv('source_file', 'destination_file', function (err) {
    if (err) {
        throw err;
    }
    console.log('file moved successfully');
});

回答by Sindre Sorhus

I made a Node.js module that just handles it for you. You don't have to think about whether it's going to be moved within the same partition or not. It's the fastest solution available, as it uses the recent fs.copyFile()Node.js API to copy the file when moving to a different partition/disk.

我制作了一个 Node.js 模块来为你处理它。您不必考虑它是否会在同一分区内移动。这是最快的解决方案,因为它使用最新的fs.copyFile()Node.js API 在移动到不同的分区/磁盘时复制文件。

Just install move-file:

只需安装move-file

$ npm install move-file

Then use it like this:

然后像这样使用它:

const moveFile = require('move-file');

(async () => {
    await moveFile(fromPath, toPath);
    console.log('File moved');
})();