Node.js | fs.opendirSync() Method

The fs.opendirSync() method is used to synchronously open a directory in the file system. It returns an fs.Dir object that is used to represent the directory. This object contains various methods that can be used to accessing the directory.

Syntax:

fs.opendirSync( path[, options] )

Parameters: This method accepts two parameters as mentioned above and described below:

  • path: It is a String, Buffer or URL that denotes the path of the directory that has to be opened.
  • options: It is a String or an object that can be used to specify optional parameters that will affect the output. It has two optional parameters:
    • encoding: It is a String that specifies the encoding for the path when opening the directory and for subsequent read operations. The default value is ‘utf8’.
    • bufferSize: It is a number that specifies the number of directory entries that are buffered internally when the directory is being read. A higher value means more performance but leads to higher memory usage. The default value is ’32’.

Return Value: It returns an fs.Dir object which represents the directory and contains various methods that can be used to access it.

Below examples illustrate the fs.opendirSync() method in Node.js:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.opendirSync() method
  
// Import the filesystem module
const fs = require('fs');
  
// Open the directory
console.log("Opening the directory");
let openedDir = fs.opendirSync("example_dir");
  
// Print the pathname of the directory
console.log("\nPath of the directory:", openedDir.path);
  
// Close the directory
console.log("\nClosing the directory");
openedDir.closeSync();

chevron_right


Output:

Opening the directory

Path of the directory: example_dir

Closing the directory

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.opendirSync() method
  
// Import the filesystem module
const fs = require('fs');
  
// Function to get current filenames
// in directory
filenames = fs.readdirSync("example_dir");
  
console.log("\nCurrent filenames in directory:");
filenames.forEach((file) => {
  console.log(file);
});
  
// Open the directory
openedDir = fs.opendirSync("example_dir");
  
// Print the pathname of the directory
console.log("\nPath of the directory:", openedDir.path);
  
// Read the files in the directory
// as fs.Dirent objects
console.log("First Dirent:", openedDir.readSync());
console.log("Next Dirent:", openedDir.readSync());
console.log("Next Dirent:", openedDir.readSync());

chevron_right


Output:

Current filenames in directory:
example1.txt
example2.txt

Path of the directory: example_dir
First Dirent: Dirent { name: 'example1.txt', [Symbol(type)]: 1 }
Next Dirent: Dirent { name: 'example2.txt', [Symbol(type)]: 1 }
Next Dirent: null

Reference: https://nodejs.org/api/fs.html#fs_fs_opendirsync_path_options




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.