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

后端 未结 5 1162
终归单人心
终归单人心 2020-11-30 04:36

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.

5条回答
  •  情书的邮戳
    2020-11-30 05:21

    One more solution to the problem.

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

    You use it like so: 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.

提交回复
热议问题