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

后端 未结 5 1159
终归单人心
终归单人心 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:23

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

提交回复
热议问题