DEV Community

Leonard Ginters
Leonard Ginters

Posted on • Edited on

7 1

Get files recursive with the Node.js File System (FS)

The File System Promises API

In the following code snippets, I'll be using the fs Promises API.
It's available to you if you're rocking at least Node.js v10.

const { promises: fs } = require("fs");
Enter fullscreen mode Exit fullscreen mode

​ 

Separating the directory entries

To be able to separate the entries, we have to explicitly ask for all information about the file type.

const entries = await fs.readdir(path, { withFileTypes: true });
Enter fullscreen mode Exit fullscreen mode

If we now want to separate the entries, we can just do this by calling the isDirectory method.

const folders = entries.filter(entry => entry.isDirectory());
const files = entries.filter(entry => !entry.isDirectory());
Enter fullscreen mode Exit fullscreen mode

Getting the files recursive

If we now combine the previously mentioned ways to separate the files, put everything in a function and call this function recursively for every subdirectory, we are able to get all files within the current directory and all subdirectories.

async function getFiles(path = "./") {
    const entries = await fs.readdir(path, { withFileTypes: true });

    // Get files within the current directory and add a path key to the file objects
    const files = entries
        .filter(entry => !entry.isDirectory())
        .map(file => ({ ...file, path: path + file.name }));

    // Get folders within the current directory
    const folders = entries.filter(entry => entry.isDirectory());

    for (const folder of folders)
        /*
          Add the found files within the subdirectory to the files array by calling the
          current function itself
        */
        files.push(...await getFiles(`${path}${folder.name}/`));

    return files;
}
Enter fullscreen mode Exit fullscreen mode

AWS Industries LIVE! Stream

Business benefits of the cloud

Stream Industries LIVE! to explore innovative cloud solutions for modern businesses.

Learn More

Top comments (2)

Collapse
 
vaibhav_arora__ profile image
Vaibhav Arora • Edited

Change first 3 lines with -

const { readdir } = require('fs/promises');
async function getFiles(path = "./") {
const entries = await readdir(path, { withFileTypes: true });

Collapse
 
vaibhav_arora__ profile image
Vaibhav Arora

There is error in following line -
const entries = await fs.readdir(path, { withFileTypes: true });

It needs callback

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay