Open In App

Node.js fs.Dir.readSync() Method

Last Updated : 06 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The fs.Dir.readSync() method is an inbuilt application programming interface of class fs.Dir within File System module which is used to read each next dirent of directory.
Syntax: 
 

const dir.readSync()

Parameter: This method does not accept any parameter.
Return Value: This method returns the dirent of the directory.
Below programs illustrates the use of fs.Dir.readSync() method in Node.js:
Example 1: 
Filename: index.js 
 

javascript




// Node.js program to demonstrate the
// dir.readSync() method
const fs = require('fs');
 
// Initiating async function
async function stop(path) {
 
  // Creating and initiating directory's
  // underlying resource handle
  const dir = await fs.promises.opendir(path);
   
  // Synchronously reading the directory's
  // underlying resource handle
  const dirent = dir.readSync();
 
  // Display the result
  console.log(dirent.name);
}
 
// Catching error
stop('./').catch(console.error);


Run index.js file using the following command: 

node index.js

Output: 

abcd.cer

Example 2: 
Filename: index.js 
 

javascript




// Node.js program to demonstrate the
// dir.readSync() method
const fs = require('fs');
   
// Initiating async function
async function stop(path) {
  
  let dir = null;
  
  try {
 
  // Creating and initiating directory's
  // underlying resource handle
  dir = await fs.promises.opendir(
      new URL('file:///F:/java/'));
 
  // Synchronously reading the directory's
  // underlying resource handle
  // using readSync() method
  for(var i = 0 ; i <= 3 ; i ++ ) {
    var dirent = dir.readSync();
 
    // Display the result
    console.log(dirent.name);
  }
 
  } finally {
  
    if (dir) {
 
      // Display the result
      console.log("dir is closed successfully");
 
      // Synchronously closeSyncing the
      // directory's underlying resource
      // handle
      const promise = dir.closeSync()
    }
  }
}
   
// Catching error
stop('./').catch(console.error);


Run index.js file using the following command: 

node index.js

Output: 

abcd.cer
cert.cer
certfile.cer
certificate1.cer
dir is closed successfully

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_dir_readsync
 



Previous Article
Next Article

Similar Reads

Node.js console.dir() Method
The console.dir() method is used to get the list of object properties of a specified object. These object properties also have child objects, from which you can inspect for further information. Syntax: console.dir( object ) Parameters: This method accepts single parameter which holds the object element. Example 1: &lt;!DOCTYPE html&gt; &lt;html&gt;
1 min read
Node.js fs.Dir.read() Method
The fs.Dir.read() method is an inbuilt application programming interface of class fs.Dir within File System module which is used to read each next directory (dirent) one by one asynchronously. Syntax: const fs.Dir.read(callback) Parameter: This method takes a callback function as a parameter that has the following arguments. err: if any error occur
2 min read
Node.js fs.Dir.close() Method
The fs.Dir.close() method is an inbuilt application programming interface of class fs.Dir with in File System module which is used to close the directory's underlying resource handle asynchronously. Syntax: const dir.close() Parameter: This method does not accept any parameter.Return Value: This method returns the promise which is just an enhanceme
2 min read
Node.js fs.Dir.closeSync() Method
The fs.Dir.closeSync() method is an inbuilt application programming interface of class fs.Dir within File System module which is used to close the directory's underlying resource handle synchronously. Syntax: const dir.closeSync() Parameter: This method does not accept any parameter.Return Value: This method returns an undefined object Below progra
2 min read
Node.js fs.Dir.path() Method
The fs.Dir.path() method is an inbuilt application programming interface of class fs.Dir within the File System module which is used to get the path of the directory. Syntax: const dir.path Parameter: This method does not accept any parameter. Return Value: This method returns the path of the directory. Example 1: The below programs illustrate the
2 min read
PHP | dir() (Get instance of the Directory)
The dir() function in PHP used to find the instance of a Directory class. This function read directory, which includes the following: The given directory is opened. The two properties handle and path of dir() are available. The handle property can be used with other directory functions such as readdir(), rewinddir(), closedir(). The path property i
2 min read
PHP | dir() Function
The dir() function in PHP is an inbuilt function which is used to return an instance of the Directory class. The dir() function is used to read a directory, which includes the following: The given directory is opened. The two properties handle and path of dir() are available. Both handle and path properties have three methods: read(), rewind(), and
2 min read
HTML DOM dir Property
The DOM dir Property is used to set or return the value of a dir attribute in an Element. It defines the direction of the text in an Element Content. Syntax HTMLElementObject.dir Return Values: It returns the direction of the text. HTMLElementObject.dir = "ltr|rtl|auto" Properties: ltr: It is a default value and is used to return the text-direction
2 min read
HTML | DOM Bdo dir Property
The Bdo dir Property in HTML DOM is used to set or return the value of the text direction of an element. The dir attribute defines the text direction of an element. Syntax: It is used to return the text direction property. bdoObject.dir It is used to set the text direction property. bdoObject.dir = "ltr|rtl" Property Values: ltr: It defines the lef
2 min read
HTML | &lt;dir&gt; compact Attribute
The HTML &lt;dir&gt; compact Attribute is used to define the list should be smaller than the normal by reducing the space between the list items and the indentation of the list. It is a Boolean attribute.Syntax: &lt;dir compact&gt; Note: The HTML &lt;dir&gt; compact attribute is not supported by HTML 5 instead of using this we can use CSS line-heig
1 min read