Home  >  Article  >  Web Front-end  >  Instructions for using the fs.rename method in node.js_node.js

Instructions for using the fs.rename method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:26:431414browse

Method description:

Modify the file name to change the file storage path.

Grammar:

Copy code The code is as follows:

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:

Copy code The code is as follows:

//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:

Copy code The code 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);
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn