Tuesday, December 27, 2022
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 | Dec, 2022


Picture by Dziana Hasanbekava

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

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

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

The above technique, 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 perform over than any doable exception. Node.js doesn’t be sure 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 technique. If the goal listing doesn’t exist then an exception can be thrown as a result of the goal listing should exist (the tactic is not going to mechanically create the goal listing). Due to this fact, earlier than utilizing the above technique, person should validate whether or not the goal listing definetly exists or not? If the goal listing doesn’t exists, person may use fs.mkdir()or fs.mkdirSync()to create the goal listing. copyFile() technique can’t copy directories.

On this means, learn the content material of the supply file after which write to the goal file. If the content material of the supply file needs to be modified throughout copying, this technique is appropriate

The drawback of this technique is identical because the above copyFile() technique. readFile() technique is used to learn the contents of the supply file and writeFile() technique can solely write recordsdata in current directories. Through the use of this technique we are able to’t copy directories. The content material could be modified whereas being copied is the benefit of utilizing this technique.

readFile() technique and writeFile() technique are the entire block of operation knowledge. If the file dimension is massive the above technique will put extra pressure on system sources. createReadStream() and createWriteStream() is to make use of the best way of stream to govern knowledge.

fs.createReadStream('./knowledge.txt').pipe(fs.createWriteStream(`./information.txt`));

The brand new fs.cp() technique has been added since model 16.7.0 of Node.js. Through the use of this technique, the whole listing construction together with subdirectories and recordsdata could be copied asynchronously from src to dest. fs.cp() technique can copy both a file or a listing. The configuration’s recursive property must be set to true if a listing copy is required.

To repeat recordsdata

To repeat the listing, together with subdirectories and recordsdata.

As you may see, this fs.cp() technique is significantly better than the above 3 strategies.

  1. The dest listing doesn’t should be required to exist. The dest listing can be mechanically created if it doesn’t exist already (whatever the stage of a listing)
  2. You’ll be able to fully copy recordsdata in the whole folder, together with subdirectories, with out recursively copying them individually.

When you will use this technique very first thing first you’ll want to verify the Node.js model!

What if you wish to copy each file within the folder however you solely have a decrease model of Node.js? We will recursively copy some recordsdata along with the native cp command for Linux, which is roofed within the subsequent part:

Find out how to use:

copyDir('./part', './web page/house');

To execute Linux native instructions, we might use the exec or spawn instructions in child_process. To repeat recordsdata or directories, the cp command in Linux is used.

You would ready to make use of the above 5 strategies in case you are utilizing the most recent node model. Utilizing the fs module within the node, I’ve shared the quickest methods to repeat a file/listing. We totally regarded on the asynchronous strategies that we accessed by means of the fs module of Node.js.

Join our free weekly publication. Observe us on Twitter, LinkedIn, YouTube, and Discord.

Trying to scale your software program startup? Try Circuit.





Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments