Saturday, November 21, 2026
HomeSoftware EngineeringEasy methods to copy information and folders in Node.js? | by Sabesan...

Easy methods to copy information and folders in Node.js? | by Sabesan Sathananthan | Dec, 2022

[ad_1]

Photograph by Dziana Hasanbekava

In Node.js, there are a number of methods to repeat information., 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 best motion.

fs.copyFile('./information.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 aren’t 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 to delete the goal file if an error occurs after opening the goal file for writing.

There’s a drawback after we use the above technique. If the goal listing doesn’t exist then an exception shall be thrown as a result of the goal listing should exist (the strategy won’t robotically 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 method, 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 information in present directories. Through the use of this technique we will’t copy directories. The content material may be modified whereas being copied is the benefit of utilizing this technique.

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

fs.createReadStream('./information.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 complete listing construction together with subdirectories and information may 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 information

To repeat the listing, together with subdirectories and information.

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

  1. The dest listing doesn’t have to be required to exist. The dest listing shall be robotically created if it doesn’t exist already (whatever the stage of a listing)
  2. You may fully copy information in the complete folder, together with subdirectories, with out recursively copying them individually.

When you’re going to use this technique very first thing first it’s worthwhile 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 are able to recursively copy some information along with the native cp command for Linux, which is roofed within the subsequent part:

Easy methods to use:

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

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

You possibly can ready to make use of the above 5 strategies if you’re 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 completely seemed on the asynchronous strategies that we accessed by the fs module of Node.js.

Join our free weekly e-newsletter. Observe us on Twitter, LinkedIn, YouTube, and Discord.

Trying to scale your software program startup? Try Circuit.



[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments