Node.js fs.dirent.name Property
Last Updated :
07 Feb, 2023
The fs.Dirent.name property is an inbuilt application programming interface of class fs.Dirent within File System module which is used to provide the name of the particular dirent.
Syntax:
const dirent.name
Parameter: This property does not accept any parameter.
Return Value: This property returns the name of the particular dirent.
Below programs illustrates the use of fs.dirent.name property in Node.js:
Example 1: Filename: index.js
Javascript
const fs = require( 'fs' );
async function stop(path) {
const dir = await
fs.promises.opendir(path);
for ( var i = 0; i <= 6; i++) {
const value = (dir.readSync()).name;
console.log(value);
}
}
stop( './' ). catch (console.error);
|
Run index.js file using the following command:
node index.js
Output:
abcd.cer
cert.cer
certfile.cer
certificate1.cer
example.txt
features
filename.txt
Example 2: Filename: index.js
Javascript
const fs = require( 'fs' );
async function stop(path) {
let dir = null ;
try {
dir = await fs.promises.opendir(
new URL('file:
for ( var i = 0; i <= 3; i++) {
const value = (dir.readSync()).name;
console.log(dir.readSync());
console.log(value);
}
} finally {
if (dir) {
console.log( "dir is closed successfully" );
const promise = dir.closeSync();
}
}
}
stop('./'). catch (console.error);
|
Run index.js file using the following command:
node index.js
Output:
Dirent { name: 'books.txt', [Symbol(type)]: 1 }
abcd.cer
Dirent { name: 'certfile.cer', [Symbol(type)]: 1 }
cert.cer
Dirent { name: 'example.com_index.html', [Symbol(type)]: 1 }
certificate1.cer
Dirent { name: 'features', [Symbol(type)]: 2 }
example.txt
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_dirent_name
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...