01/19/2023

Node JS Read File Directory

Reading a directory in Node JS can be useful for many reasons, here's a quick way to get all the file paths in an array.

function getFiles(dir) {
    return fs.readdirSync(dir).flatMap((item) => {
        const path = `${dir}/${item}`;
        if (fs.statSync(path).isDirectory()) {
            return getFiles(path);
        }

        return path;
    });
}

This is a JavaScript function that takes in a single argument, dir, which is a string representing a directory path. The function uses the fs (file system) module to read the contents of the directory specified by dir using fs.readdirSync(dir). The resulting array of items is then passed to the Array.prototype.flatMap() method, which applies a function to each element of the array, and then flattens the resulting arrays into a single array.

The function passed to flatMap checks whether each item in the directory is a directory or a file. If it's a directory, it calls the getFiles function recursively with the path to that directory as an argument, otherwise, it returns the file's path. The result is an array of all the file paths in the directory and all its subdirectories.

Example

const dir = '/Users/johndoe/documents';
const files = getFiles(dir);
console.log(files);
Output
['/Users/johndoe/documents/notes.txt',
'/Users/johndoe/documents/projects/project1/tasks.txt',
'/Users/johndoe/documents/projects/project1/report.pdf',
'/Users/johndoe/documents/projects/project2/proposal.docx',
'/Users/johndoe/documents/photos/vacation/IMG_0123.jpg',
'/Users/johndoe/documents/photos/vacation/IMG_0124.jpg',
'/Users/johndoe/documents/photos/vacation/IMG_0125.jpg']
Back

Comments


Be the first to comment!

Read More

Hero
Hero

Generating Images Based on a URL Slug

Welcome to our tutorial on dynamically generating images using Node.js! In this post, we'll be exploring a technique for creating images on the fly based on the URL slug of a webpage. This can be a powerful tool for creating personalized, dynamic content for your website or application. Whether you're looking to create unique product images, customized social media graphics, or any other t...

DOM Structure
DOM Structure

What is HTML DOM?

The HTML Document Object Model (DOM) is an integral part of web development and is used to create dynamic web pages. It is a hierarchical tree-like structure that contains the content of the webpage, making it possible for developers to access and manipulate the content. Each element of the webpage is a node, with a unique ID and a set of attributes. The root node of the DOM ...

Node JS Express Server

Even though Node JS has a built in HTTP request handler using a web server framework can reduce the amount of code and increase the reliability of your site. There are many options to ...

Generating Random Colors on a Range

If you want to add some visual interest to your website, consider incorporating random colors. While using the Math.random() function to generate colors can be a quick and easy option, it may result in some less-than-appealing hues. An alternative approach is to create a set...