Software Engineering

Tips on how to copy information and folders in Node.js? | by Sabesan Sathananthan | Dec, 2022

Tips on how to copy information and folders in Node.js? | by Sabesan Sathananthan | Dec, 2022
Written by admin


Photograph by Dziana Hasanbekava

In Node.js, there are a number of methods to repeat information., let’s check out the potential 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/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 aren’t any args handed to the callback perform over than any potential exception. Node.js doesn’t make sure 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 once we use the above methodology. If the goal listing doesn’t exist then an exception shall be thrown as a result of the goal listing should exist (the tactic won’t routinely create the goal listing). Due to this fact, earlier than utilizing the above methodology, person should validate whether or not the goal listing definetly exists or not? If the goal listing doesn’t exists, person might use fs.mkdir()or fs.mkdirSync()to create the goal listing. copyFile() methodology can’t copy directories.

On this approach, learn the content material of the supply file after which write to the goal file. If the content material of the supply file must be modified throughout copying, this methodology is appropriate

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

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

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

The brand new fs.cp() methodology has been added since model 16.7.0 of Node.js. By utilizing this methodology, the complete listing construction together with subdirectories and information could be copied asynchronously from src to dest. fs.cp() methodology 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() methodology 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 routinely created if it doesn’t exist already (whatever the degree of a listing)
  2. You may utterly copy information in the complete folder, together with subdirectories, with out recursively copying them individually.

When you will use this methodology very first thing first it’s worthwhile to affirm 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 information along with the native cp command for Linux, which is roofed within the subsequent part:

Tips on how to use:

copyDir('./element', './web page/dwelling');

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

You might 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 totally appeared on the asynchronous strategies that we accessed via the fs module of Node.js.

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

Seeking to scale your software program startup? Try Circuit.



About the author

admin

Leave a Comment