Method description:
Modify the file name to change the file storage path.
grammar:
The code copy is as follows:
fs.rename(oldPath, newPath, [callback(err)])
Since this method belongs to the FS module, it is necessary to introduce the FS module before use (var fs= require("fs") )
Receive parameters:
oldPath original path
newPath new path
callback, passing an err exception parameter
example:
The code copy is as follows:
//Change the file name in the directory:
var fs = require('fs');
fs.rename('125.txt','126.txt', function(err){
if(err){
throw err;
}
console.log('done!');
})
//File name change + Move under different paths: (The new path must already exist, if the path does not exist, it will return an exception)
var fs = require('fs');
fs.rename('125.txt','new/126.txt', function(err){
if(err){
throw err;
}
console.log('done!');
})
Source code:
The code copy is as follows:
fs.rename = function(oldPath, newPath, callback) {
callback = makeCallback(callback);
if (!nullCheck(oldPath, callback)) return;
if (!nullCheck(newPath, callback)) return;
binding.rename(pathModule._makeLong(oldPath),
pathModule._makeLong(newPath),
callback);
};