Зміст курсу
Backend Development with Node.js and Express.js
Backend Development with Node.js and Express.js
Working 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.
Дякуємо за ваш відгук!