Node.js fs.Dir.closeSync() Method
Last Updated :
30 Jan, 2023
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 programs illustrates the use of fs.Dir.closeSync() method.
Example 1:
Filename: index.js
Javascript
const fs = require( 'fs' );
async function stop(path) {
const dir = await fs.promises.opendir(path);
console.log( "All the direct before operation :- " );
for ( var i = 0; i <= 6; i++) {
console.log(((dir.readSync())).name);
}
const promise = dir.closeSync();
console.log( "dir is closed successfully" );
}
stop( './' ). catch (console.error);
|
Run index.js file using the following command:
node index.js
Output:
All the direct before operation :-
abcd.cer
books.txt
cert.cer
certfile.cer
certificate1.cer
example.txt
features
dir is closed successfully
Example 2:
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(
new URL('file:
console.log( "All the direct before operation :- "
+ ((dir.readSync())));
pathval = dir.path;
} finally {
if (dir) {
console.log( "Path :- " + pathval);
console.log( "All the direct before operation :- "
+ ((dir.readSync())));
console.log( "dir is closed successfully" );
await dir.closeSync();
}
}
}
funct('./'). catch (console.error);
|
Run index.js file using the following command:
node index.js
Output:
All the direct before operation:- [object Object]
Path :- F:
All the direct before operation:- [object Object]
dir is closed successfully
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_dir_closesync
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...