Logo

dev-resources.site

for different kinds of informations.

List filenames recursively in a directory using this utility function.

Published at
1/16/2025
Categories
filesystem
node
opensource
recursive
Author
ramunarasinga-11
Author
16 person written this
ramunarasinga-11
open
List filenames recursively in a directory using this utility function.

In this article, we will review a function named listRecursively found in unbuild/src/utils.ts

export function listRecursively(path: string): string[] {
  const filenames = new Set<string>();
  const walk = (path: string): void => {
    const files = readdirSync(path);
    for (const file of files) {
      const fullPath = resolve(path, file);
      if (statSync(fullPath).isDirectory()) {
        filenames.add(fullPath + "/");
        walk(fullPath);
      } else {
        filenames.add(fullPath);
      }
    }
  };
  walk(path);
  return [...filenames];
}
Enter fullscreen mode Exit fullscreen mode

This codesnippet does below:

  • Initialise filenames to a Set
const filenames = new Set<string>();
Enter fullscreen mode Exit fullscreen mode

Using a Set prevents duplicate file names, if you wish to include duplicate files names, you are better off using an array.

I study large open-source projects and provide insights, give my repository a star.

  • Define walk function with path parameter
const walk = (path: string): void => {
Enter fullscreen mode Exit fullscreen mode
  • Get the list of files/directorites
const files = readdirSync(path);
Enter fullscreen mode Exit fullscreen mode

readdirSync is an API used to read the contents of the directory. Read more about readdirSync.

Image description

  • Loop through files and get full path
for (const file of files) {
      const fullPath = resolve(path, file);
Enter fullscreen mode Exit fullscreen mode
  • Check if the path is a directory
// statSync provides an API to check if the given path is a directory.
if (statSync(fullPath).isDirectory()) {
  // Add the path to filenames set
  // filenames is a set and add is used add the fullPath
  filenames.add(fullPath + "/");
  // Call walk function recursively
  walk(fullPath);
} else {
  // filenames is a set and add is used add the fullPath
  filenames.add(fullPath);
}
Enter fullscreen mode Exit fullscreen mode
  • Call the walk function.
 walk(path);
Enter fullscreen mode Exit fullscreen mode
  • Return the file names
return [...filenames];
Enter fullscreen mode Exit fullscreen mode

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on interesting projects. Send me an email at [email protected]

My Github β€” https://github.com/ramu-narasinga

My website β€” https://ramunarasinga.com

My Youtube channel β€” https://www.youtube.com/@thinkthroo

Learning platform β€” https://thinkthroo.com

Codebase Architecture β€” https://app.thinkthroo.com/architecture

Best practices β€” https://app.thinkthroo.com/best-practices

Production-grade projects β€” https://app.thinkthroo.com/production-grade-projects

References

  1. https://github.com/unjs/unbuild/blob/main/src/utils.ts#L54

  2. https://nodejs.org/api/fs.html#fsreaddirsyncpath-options

  3. https://nodejs.org/api/fs.html#fsstatsyncpath-options

filesystem Article's
30 articles in total
Favicon
List filenames recursively in a directory using this utility function.
Favicon
Where Does Deleted Data Go? Unveiling the Secrets of File Deletion and Overwriting
Favicon
Amazon FSx for NetApp ONTAP - Expert Storage for any workload
Favicon
Understanding the Linux Filesystem, Root File System, and EXT File System
Favicon
How to fix RHEL file system
Favicon
Understanding the Linux Filesystem: A Quick Guide
Favicon
Introducing Cora: A Powerful File Concatenation Tool for Developers
Favicon
Hitchhikers guide to building a distributed filesystem in Rust. The very beginning…
Favicon
Understanding Where Deleted Files Go After Deleting them from Recycle Bin and How to Recover Them
Favicon
I wrote a File System CLI in Rust
Favicon
La extravagante posibilidad de los espacios en los nombres de archivos.
Favicon
Starting with C
Favicon
Getting the list of files and their info
Favicon
Processing flags
Favicon
Sorting and formatting the output. The Finale.
Favicon
Command, file types and flags
Favicon
Unit test in Laravel by example
Favicon
What is File Manipulation?
Favicon
you are not the owner so you cannot change the permissions Error in Linux
Favicon
Setting Up OpenZFS on Rocky Linux
Favicon
How to extend an EBS volume in AWS and then grow EFS Filesystem
Favicon
Efficient File Naming Systems for Better File Management
Favicon
Improving the .NET API to work with the structure of the file system. Part 2. Manipulate filesystem objects.
Favicon
Improving the .NET API to work with the structure of the file system. Part 1. Enumerate filesystem objects.
Favicon
How to format SD Card to APFS on Mac
Favicon
What are linux inodes?
Favicon
Block and Filesystem side-by-side with K8s and Aerospike
Favicon
How to copy lots of data fast?
Favicon
EFS vs. FSx for ONTAP
Favicon
Use Inflint to follow files and folders convention

Featured ones: