The files present in a directory can be displayed using two approaches in Node.js that are discussed below:
Method 1: Using fs.readdirSync() method: The fs.readdirSync() is a method that is available in the file system module of Node.js. It is used for reading the contents of a directory. It returns an array of file paths, buffers, or fs.Dirent objects.
The returned array of files can be looped through using the forEach() loop and the file names can be displayed from it. This allows one to display all the filenames present in the directory.
The asynchronous variation of the method fs.readdir() can also be used in place of this function that returns a callback with the files instead of the files themselves.
Example:
const fs = require("fs");
let directory_name = "example_dir";
let filenames = fs.readdirSync(directory_name);
console.log("\nFilenames in directory:");
filenames.forEach((file) => {
console.log("File:", file);
});
|
Output:
Filenames in directory:
File: file_a.txt
File: file_b.txt
File: file_c.txt
Method 2: Using the fs.opendirSync() method: The fs.opendirSync() method is available in the file system module of Node.js. It can be used to read the contents of a directory. It returns an fs.Dir object that represents the given directory.
The fs.Dir object can be used to access the files in that directory using the readSync() method. This method returns an fs.Dirent object that contains the representation of the directory entry. This object has a name property that can be used to get the file name that this fs.Dirent object refers to. This name can be accessed and displayed to this user. The readSync() method will automatically read the next directory entry and return null when no more entries exist. This can be used to continuously loop through the entries and get all the file names using a while loop.
The asynchronous variation of the method fs.opendir() can also be used in place of this function that returns a callback with the fs.Dir object instead of the object itself.
Example:
const fs = require("fs");
let directory_name = "example_dir";
let openedDir = fs.opendirSync(directory_name);
console.log("\nPath of the directory:", openedDir.path);
console.log("Files Present in directory:");
let filesLeft = true;
while (filesLeft) {
let fileDirent = openedDir.readSync();
if (fileDirent != null)
console.log("Name:", fileDirent.name);
else filesLeft = false;
}
|
Output:
Path of the directory: example_dir
Files Present in directory:
Name: file_a.txt
Name: file_b.txt
Name: file_c.tx
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
21 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...