Method description:
Modify the file name to change the file storage path.
Grammar:
fs.rename(oldPath, newPath, [callback(err)])
Since this method belongs to the fs module, the fs module needs to be introduced before use (var fs= require(“fs”) )
Receive parameters:
oldPath original path
newPath New path
callback callback, passing an err exception parameter
Example:
//Rename files in the same directory:
var fs = require('fs');
fs.rename('125.txt','126.txt', function(err){
if(err){
throw err;
}
console.log('done!');
})
//Rename files under different paths. Move: (The new path must already exist. If the path does not exist, an exception will be returned)
var fs = require('fs');
fs.rename('125.txt','new/126.txt', function(err){
if(err){
throw err;
}
console.log('done!');
})
Source code:
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);
};