Directory Inspection Tool
This chapter presents you with a challenge: creating an advanced console app named DirInspect Pro. This app will empower you to thoroughly analyze any directory and gain insightful statistics about its files and subdirectories.
🏆 Challenge Awaits
Picture a scenario where you must navigate a labyrinth of folders containing crucial files and data. DirInspect Pro is your ally in this journey, providing comprehensive insights into the directory's structure and contents.
🚀 The Resulting App
Get ready to wield DirInspect Pro's capabilities. The app will furnish you with critical information, such as
- The total number of items;
- The aggregate size of all items;
- The largest file's name and size;
- The detailed list of individual file names and sizes.
Two Paths to Choose
You have two paths ahead.
- The first is to tackle this challenge head-on, honing your skills without guidance;
- The second is to follow a helpful guide that guarantees your success.
Whichever path you choose, you're in for a rewarding journey culminating in creating a captivating and functional console app.
Masterplan
- 👉 Step 1: Import Required Modules;
- 👉 Step 2: Define getStats Function;
- 👉 Step 3: Define analyzeFile Function;
- 👉 Step 4: Define analyzeDirectory Function;
- 👉 Step 5: Define main Function and Invoke;
- 🎉 Conclusion;
- 🏁 Full App Code.
Step 1: Import Required Modules
To embark on this adventure, you'll need the right tools. Begin by importing two key modules: fs.promises
to manage the file system asynchronously and path
to handle file paths effectively.
jsconst fs = require("fs").promises;const path = require("path");
Step 2: Define getStats Function
The asynchronous function, getStats
, takes a file or directory path as an argument and attempts to retrieve its statistics using fs.stat
.
- If successful, it returns the statistics;
- If an error occurs, it logs an error message and returns
null
.
jsasync function getStats(filePath) {try {const stats = await fs.stat(filePath);return stats;} catch (err) {console.error("Error getting stats:", err.message);return null;}}
Step 3: Define analyzeFile Function
The analyzeFile
function uses the getStats
function to obtain statistics for a file. If statistics are available (not null
), it returns an object containing the file's name (extracted using path.basename
) and its size.
jsasync function analyzeFile(filePath) {const stats = await getStats(filePath);if (!stats) return null;return {name: path.basename(filePath),size: stats.size,};}
Step 4: Define analyzeDirectory Function
The analyzeDirectory
function takes a directory path as an argument and comprehensively analyzes its contents.
- It begins by reading the items within the directory using
fs.readdir
and then iterates through each item; - For each item, it constructs the full path using
path.join
and retrieves its statistics using thegetStats
function; - If the
stats
indicate that the item is a file, it updates file-related statistics; - If the item is a subdirectory, it recursively calls the
analyzeDirectory
function to analyze its contents and then aggregates the statistics.
jsasync function analyzeDirectory(directoryPath) {try {const items = await fs.readdir(directoryPath);let totalItems = 0;let totalFiles = 0;let totalSize = 0;let largestFile = { name: "", size: 0 };let fileList = [];for (const item of items) {const itemPath = path.join(directoryPath, item);const stats = await getStats(itemPath);if (!stats) continue;totalItems++;if (stats.isFile()) {totalFiles++;totalSize += stats.size;if (stats.size > largestFile.size) {largestFile = { name: item, size: stats.size };}fileList.push({ name: item, size: stats.size });} else if (stats.isDirectory()) {const subDirectoryStats = await analyzeDirectory(itemPath);totalItems += subDirectoryStats.totalItems;totalFiles += subDirectoryStats.totalFiles;totalSize += subDirectoryStats.totalSize;if (subDirectoryStats.largestFile.size > largestFile.size) {largestFile = subDirectoryStats.largestFile;}fileList = fileList.concat(subDirectoryStats.fileList);
Step 5: Define main Function and Invoke
The main
function is the entry point of the script. It specifies the directory path to analyze (in this case, ./docs
), calls the analyzeDirectory
function to obtain the statistics of the directory and its contents, and then outputs the collected information.
The function prints out
- The total number of items;
- The total number of files;
- The total size;
- The details about the largest file;
- The list of files in the directory.
jsasync function main() {const directoryPath = "./docs";const directoryStats = await analyzeDirectory(directoryPath);console.log("Directory Analysis:");console.log("Total items:", directoryStats.totalItems);console.log("Total files:", directoryStats.totalFiles);console.log("Total size (bytes):", directoryStats.totalSize);console.log("Largest file:",directoryStats.largestFile.name,"Size:",directoryStats.largestFile.size,"bytes");console.log("\nFile List:");for (const file of directoryStats.fileList) {console.log(file.name, "Size:", file.size, "bytes");}}main();
🎉 Conclusion: Mastered Skills
With DirInspect Pro, you've mastered the art of analyzing directories like a pro. This console app showcases your ability to extract file statistics, handle errors seamlessly, and unveil meaningful insights about files and subdirectories within a specified directory.
👨💻 Full App Code
jsconst fs = require("fs").promises;const path = require("path");async function getStats(filePath) {try {const stats = await fs.stat(filePath);return stats;} catch (err) {console.error("Error getting stats:", err.message);return null;}}async function analyzeFile(filePath) {const stats = await getStats(filePath);if (!stats) return null;return {name: path.basename(filePath),size: stats.size,};}async function analyzeDirectory(directoryPath) {try {const items = await fs.readdir(directoryPath);let totalItems = 0;let totalFiles = 0;let totalSize = 0;let largestFile = { name: "", size: 0 };let fileList = [];for (const item of items) {const itemPath = path.join(directoryPath, item);const stats = await getStats(itemPath);
Grazie per i tuoi commenti!