Jeremy Banks Jeremy Banks - 4 months ago 23
Node.js Question

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

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?

Answer

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