File Rename Using Node.js

By Yashwant Chavan, Views 9314, Last updated on 02-Mar-2019

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

Yashwant

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[at][gmail.com]