File rename using nodejs

By Yashwant Chavan, Views 9178, Date 02-Mar-2017

In this tutorial, You will learn how to rename file using nodejs. You can rename the files synchronously and asynchronously.

tags nodejs

syntax - Asynchronous

fs.rename(oldPath, newPath, callback)

oldPath <String> | <Buffer>

newPath <String> | <Buffer>

callback - <Function>

syntax - Synchronous

fs.renameSync(oldPath, newPath)

oldPath <String> | <Buffer>

newPath <String> | <Buffer>

file rename example (asynchronously)

var fs = require('fs');
fs.rename('c:\\dog.jpg', 'c:\\dog-rename.jpg', function(err) {
    if (err) {
        console.log('ERROR: ' + err);
        throw err;
    }
    console.log('File renamed!!');
});

output

File renamed!!

file rename example (asynchronously)

var fs = require('fs');
fs.renameSync('c:\\dog.jpg', 'c:\\dog-rename.jpg');
Yashwant Chavan

Yashwant Chavan

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan@gmail.com