. Advertisement .
..3..
. Advertisement .
..4..
Feel uncertain about readdirsync and the function it proposes? This article will alleviate that headache for you. We will explain what readdirsync is and give some illustrative examples to enhance your understanding.
Definitions of Readdirsync
What is Readdirsync? It is a type of technology used to decode the given directory uniformly. It sends back arrays with all file names of directory objects. The options argument might alter the formats in which returned files arrive.
Here is the basic syntax:
fs.readdirSync( path, options )
What are the parameters? There are two of them, described as follows:
Path: It contains the directory path from where the system decodes the content. I might be a Buffer, URL, or String.
Options: You can use this object to specify parameters that impact your method. It breaks down into two smaller parameters:
- Encoding: encoding is a string value to specify which encoding would accommodate the filenames sent to a callback argument. Its pre-set value is ‘utf8’.
- withFileTypes: withFileTypes presents boolean values to specify whether the system would send back the file as “fs.Direct” objects. Its default values are ‘false’.
Returns: It sends back a String, fs.Direct, or Buffer object array that contains all files in directories.
Examples of Readdirsync
Let’s look at the example below:
// Node.js program to demonstrate the
// fs.readdirSync() method
// Import the filesystem module
const fs = require('fs');
// Function to get current filenames
// in directory
filenames = fs.readdirSync(__dirname);
console.log("\nCurrent directory filenames:");
filenames.forEach(file => {
console.log(file);
});
// Function to get current filenames
// in directory with "withFileTypes"
// set to "true"
fileObjs = fs.readdirSync(__dirname, { withFileTypes: true });
console.log("\nCurrent directory files:");
fileObjs.forEach(file => {
console.log(file);
});
Output:
Current directory filenames:
CONTRUBUTIONS.txt
index.html
index.js
package.json
README.md
Current directory files:
Dirent { name: 'CONTRUBUTIONS.txt', [Symbol(type)]: 1 }
Dirent { name: 'index.html', [Symbol(type)]: 1 }
Dirent { name: 'index.js', [Symbol(type)]: 1 }
Dirent { name: 'package.json', [Symbol(type)]: 1 }
Dirent { name: 'README.md', [Symbol(type)]: 1 }
Another example will show you how to decode the existing folder or directory before logging a file name to your console:
const fs = require("fs");
const files = fs.readdirSync(__dirname);
console.log(files); // Log the content of the current folder
And what should you do to retrieve the contents of specific folders? In that case, pass the paths to that folder as strings. The illustration below will assess the asset folder right within your existing folder:
const fs = require("fs");
const path = "./assets";
const files = fs.readdirSync(path);
console.log(files); // Log the content of assets folder
Conclusion
This article has shown you all you need to know about readdirsync. This method is powerful and can serve as a potent technique for your more complex programs.
So try experiencing this function from time to time, and do not forget to reach us if you think there are still facets you are uncertain about. We will try our best to help!
Leave a comment