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


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() 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 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 perform over than any attainable exception. Node.js doesn’t be 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 strategy won’t routinely create the goal listing). Subsequently, 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 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 methodology is appropriate

The drawback of this methodology is similar because the above copyFile() methodology. readFile() methodology is used to learn the contents of the supply file and writeFile() methodology can solely write recordsdata in current directories. By utilizing this methodology we will’t copy directories. The content material might 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 giant the above methodology will put extra pressure on system sources. createReadStream() and createWriteStream() is to make use of the way in which of stream to govern knowledge.

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

The brand new fs.cp() methodology has been added since model 16.7.0 of Node.js. By utilizing this methodology, the whole listing construction together with subdirectories and recordsdata might 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 recordsdata

To repeat the listing, together with subdirectories and recordsdata.

As you possibly can see, this fs.cp() methodology is a lot better than the above 3 strategies.

  1. The dest listing doesn’t should 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’ll be able to utterly copy recordsdata in the whole folder, together with subdirectories, with out recursively copying them individually.

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

Learn how to use:

copyDir('./element', './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 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 totally seemed on the asynchronous strategies that we accessed by means of 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.



Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles