File rename using nodejs

 Posted On  | Yashwant Chavan 

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

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');


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0527 seconds.