Instructions for the fs.rename method in node.js

  • 2020-05-05 10:54:14
  • OfStack

method description:

Modify the file name to change the file location.

syntax:


fs.rename(oldPath, newPath, [callback(err)])

Since this method belongs to the fs module, it is necessary to introduce the fs module (var fs= require(" fs "))

before using it

receive parameters:

oldPath                          

newPath                       new path

The callback                         callback passes an err exception parameter

example:


// Rename with 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 :(new path must already exist, no path will return exception)
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);
};


Related articles: