Node.js fs.Dir.path() Method
Last Updated :
05 Apr, 2023
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 use of fs.Dir.path() method.
Filename: index.js
Javascript
const fs = require( 'fs' );
async function stop(path) {
const dir = await fs.promises.opendir(path);
console.log( "All the dirent before operation :- " );
for (let i = 0; i <= 2; i++) {
console.log(((dir.readSync())).name);
}
const pathval = dir.path;
console.log( "All the dirent after operation :- " );
for (let i = 0; i <= 2; i++) {
console.log(((dir.readSync())).name);
}
console.log( "\nPath :- " + pathval);
}
stop('./'). catch (console.error);
|
Run the index.js file using the following command:
node index.js
Output:
All the dirent before operation :-
books.txt
datastore.json
example.txt
All the dirent after operation :-
index.js
node_modules
package-lock.json
Path :- ./
Example 2: The below programs illustrate the use of fs.Dir.path() method
Filename: index.js
Javascript
const fs = require( 'fs' );
const fsPromises = fs.promises;
let directo = './temp' ;
if (!fs.existsSync(directo)) {
fs.mkdirSync(directo);
}
async function funct(path) {
let dir = null ;
let pathval = null ;
try {
dir = await fs.promises.opendir('./ ');
console.log("All the dirent before operation :- " +
((dir.readSync())));
// Getting the path of the directory
// by using path() method
pathval = dir.path;
} finally {
if (dir) {
// Close the file if it is opened.
console.log("Path :- " + pathval);
console.log("All the dirent after operation :- " +
((dir.readSync())));
await dir.close();
}
}
}
funct(' ./'). catch (console.error);
|
Run the index.js file using the following command:
node index.js
Output:
All the dirent before operation :- [object Object]
Path :- ./
All the dirent after operation :- [object Object]
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_dir_path
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...