Thursday 18 October 2018

node.js: fs.rename(oldPath, newPath, callback): Rename the file asynchronously

fs.rename(oldPath, newPath, callback)
This function renames the file asynchronously.

Below table summarizes the arguments of ‘renameSync’ method.
Argument
Type
Description
oldPath
string or Buffer or URL
Current name or path of the file
newPath
string or Buffer or URL
New name or path of the file
Callback
Function
This function takes one argument ‘err’ and it is called when rename call is executed. On failure cases ‘err’ represents the stack trace.

HelloWorld.js
var fs = require('fs');

var oldPath = 'input.txt'; // Assume input.txt file is already exist.
var newPath = 'output.txt';

fs.rename(oldPath, newPath, (error) =>{
 if(error){
  console.log(`Error occured while renaming, ${error}`)
 }else{
  console.log('Renamed successfuly');
 }
})



Previous                                                 Next                                                 Home

No comments:

Post a Comment