Tutorial: Effectively copy files to folder with NodeJS

Rajdeep Chandra
3 min readDec 27, 2022

--

While scanning through few demos on how to copy files from one folder to another folder I got many results but most of them are use case specific. While I was looking for something more generic and sequentials I was not able to find it quick enough. So I tried to create a generic function which will deep copy files from one folder to another even if the folder doesnt exist. I mainly created this method to cover most of the corner cases which backend developers mind find during the development.
Let’s go through the below code line and line and understand what it is doing.

First and foremost we require 2 modules.

1. Install Glob Module

Install via NPM 
npm i glob

OR

Install via Yarn
yarn add glob

What Glob Module does?

The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order. No tilde expansion is done, but * , ? , and character ranges expressed with [] will be correctly matched. This is done by using the os.

We have used glob module to copy all matching files or folders to the remote directory. If you want to exclude few files from the source folder you can do so via:

glob(`/globals/*.css`, { ignore: 'someFile.css' }, function (er, files) { })

Source Folder:

SourceFolder
- cat.css
- dog.css
- man.css

So we would want to copy all source files inside SourceFolder directory and paste them in DestinationFolder(for e.g) excluding cat.css file

const glob = require('glob');
const fs = require('fs');

function copyResources(srcDir, destDir) {
// Use glob to get a list of all the files in the source directory
glob(`${srcDir}`, {ignore: 'man.css' }, (err, files) => {
if (err) {
// Handle the error
}

// Iterate over the list of files
files.forEach((srcPath) => {
// Construct the full path to the destination file
const destPath = srcPath.replace(srcDir, destDir);

// Check if the file is a directory
fs.stat(srcPath, (err, stats) => {
if (err) {
// Handle the error
}

if (stats.isDirectory()) {
// If the file is a directory, create it in the destination
fs.mkdir(destPath, { recursive: true }, (err) => {
if (err) {
// Handle the error
}
});
} else {
// If the file is a regular file, copy it
fs.copyFile(srcPath, destPath, (err) => {
if (err) {
// Handle the error
}
});
}
});
});
});
}

copyResources('SourceFolder' , 'DestinationFolder')

We run over every file in ‘SourceFolder’ ignoring man.css file and the output of glob method is the list of files inside the directory.

Once we get all the files then we check if the file is a directory or just a simple file. If it is a simple file we just copy it to the destDir

If the file is a directory we create the folder in the destination folder and paste it accordingly

P.S: The source and destination folders can be anywhere in the project levels. It is not necessary that they should be on the same level.

For any clarifications: Please write your query at rajrock38@gmail.com

--

--