Monday 15 October 2018

fs: fs.readdirSync(path[, options]): list all the files in a directory

fs.readdirSync(path[, options])
This method returns an array of filenames excluding '.' and '..'.

Below table summarizes the arguments of readdirSync method.

Argument
Type
Description
path
string or Buffer or URL
Specify the path of the directory to list the contents for.
options
string or Object
Below table summarizes different options.
Option
Type
Description
encoding
string
Use this encoding scheme, while returning file names. Default value is utf8.

If the encoding is set to 'buffer', the filenames returned will be passed as Buffer objects.

HelloWorld.js
var fs = require('fs');

var path = 'C:\\Users\\Public\\demo';

var fileNames = fs.readdirSync(path);

console.log(fileNames);


Above script, return all the file names in the directory 'C:\\Users\\Public\\demo'.





Previous                                                 Next                                                 Home

No comments:

Post a Comment