Wednesday, May 29, 2024
HomeSoftware EngineeringFind out how to copy recordsdata and folders in Node.js? | by...

Find out how to copy recordsdata and folders in Node.js? | by Sabesan Sathananthan


A number of methods to repeat recordsdata in Node.js

Picture by Dziana Hasanbekava

In Node.js, there are a number of methods to repeat recordsdata., let’s check out the attainable methods and evaluation every of them. That is my forty fourth Medium article.

The copyFile() operate, which may copy a file on to the vacation spot listing, performs the only motion.

fs.copyFile('./knowledge.txt', './dest/data.txt');

The above methodology, asynchronously copies the file from src to dest. If dest is already exists then by default it’s overwritten. There are not any args handed to the callback operate over than any attainable exception. Node.js doesn’t be certain that copy operations are atomic. Node.js will try and delete the goal file if an error occurs after opening the goal file for writing.

There’s a drawback once we use the above methodology. If the goal listing doesn’t exist then an exception can be thrown as a result of the goal listing should exist (the strategy won’t routinely create the goal listing). Due to this fact, earlier than utilizing the above methodology, consumer should validate whether or not the goal listing definetly exists or not? If the goal listing doesn’t exists, consumer might use fs.mkdir()or fs.mkdirSync()to create the goal listing. copyFile() methodology can’t copy directories.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments