Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Working with Directories | Console Applications
Backend Development with Node.js and Express.js
course content

Course Content

Backend Development with Node.js and Express.js

Backend Development with Node.js and Express.js

1. Introduction
2. Console Applications
3. Express.js Framework
4. Developing REST API

bookWorking with Directories

We've learned many file manipulation techniques throughout our exploration of the FileSystem (fs) module. But directories are more than places to store files - they offer opportunities for organization, data analysis, and more.

In this chapter, we'll dive into directory manipulation and guide you on navigating directories, gathering vital statistics, processing directories, and creating a script to analyze and display directory contents.

📂 Navigating Directories with fs.readdir

The fs.readdir method asynchronously reads the contents of a directory. It returns an array of filenames. This method can be beneficial for task listing files in a folder.

Consider a scenario where we deal with extensive legal contracts, briefs, and other documents related to different cases and clients. We could create a system that extracts and lists the names of the files within each client's folder.

Code Example: Read the file names of a directory

Step by Step Explanation

📊 Obtaining Directories Statistics with fs.stat

Directories house files and hold valuable information about each file's attributes.

The fs.stat method asynchronously retrieves the stats of a file or directory. These stats include file size, permissions, timestamps, and more.

Let's obtain the statistics of each folder inside the docs folder.

Code Example: Get directories names and statistics

Step by Step Explanation

🧐 Quiz Time

Let's put your knowledge of the FileSystem (fs) module to the test with a few questions related to directory manipulation.

1. Which method can be used to list the contents of a directory?
2. What is the purpose of the `fs.stat` method?
Which method can be used to list the contents of a directory?

Which method can be used to list the contents of a directory?

Select the correct answer

What is the purpose of the `fs.stat` method?

What is the purpose of the fs.stat method?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 9
We're sorry to hear that something went wrong. What happened?
some-alt