10 examples of 'fs readdir' in JavaScript

Every line of 'fs readdir' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
30function readdir(dir) {
31 return new Promise((resolve, reject) => {
32 fs.readdir(dir, (err, files) => {
33 if (err) {
34 reject(err);
35 return;
36 }
37 resolve(files);
38 });
39 });
40}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

70function readDir(dir, callback) {
71 dropbox.readdir(dir, function(err, stringEntries, dirStat, entries) {
72 if (err) {
73 return callback(err);
74 }
75 async.parForEach(entries, function(entry, next) {
76 // if (entry.name[0] === ".") {
77 // return next();
78 // }
79 if (entry.isFolder) {
80 readDir(entry.path, next);
81 } else {
82 files.push(entry.path);
83 next();
84 }
85 }, callback);
86 });
87}
3function readDir(file, optResolver, onRead) {
4 // Do nothing for now
5 onRead();
6}
77public readDir(directory: string): Q.Promise<string[]> {
78 return Q.nfcall<string[]>(this.fs.readdir, directory);
79}
15readdir(path: string, callback: Function): any {
16 return this.originalFileSystem.readdir(path, callback);
17}
47public readdirAsync(path: string): Promise<string[]> {
48 return fs_async.readdir(path);
49}
161async readdir(path, withFileTypes) {
162 this.logger.trace(`readdir(path: ${path}, withFileTypes: ${withFileTypes})`);
163 if (withFileTypes === true) {
164 return readdir(path, { withFileTypes: true });
165 }
166 return readdir(path);
167}
334async function readdir(path, options) {
335 options = getOptions(options, {});
336 path = getValidatedPath(path);
337 const result = await binding.readdir(pathModule.toNamespacedPath(path),
338 options.encoding,
339 !!options.withFileTypes,
340 kUsePromises);
341 return options.withFileTypes ?
342 getDirectoryEntriesPromise(path, result) :
343 result;
344}
120export async function readdirr(dir: string, { filter, walkerOptions }: ReadDirROptions = {}): Promise<string[]> {
121 return new Promise<string[]>((resolve, reject) => {
122 const items: string[] = [];
123
124 let rs = walk(dir, walkerOptions);
125
126 if (filter) {
127 rs = rs.pipe(through2.obj(function(obj, enc, cb) {
128 if (!filter || filter(obj)) {
129 this.push(obj);
130 }
131
132 cb();
133 }));
134 }
135
136 rs
137 .on('error', err => reject(err))
138 .on('data', item => items.push(item.path))
139 .on('end', () => resolve(items));
140 });
141}
50function _readdirRecursive(options) {
51 return readdir(options.path)
52 .then(function(dirContents) {
53 var work = _.chain(dirContents)
54 .map(function(shortName) {
55 return path.join(options.path, shortName);
56 })
57 .reject(options.filter)
58 .map(function(file) {
59 return stat(file).then(function(fstat) {
60 if (fstat.isFile()) {
61 return {
62 name: file,
63 mode: fstat.mode,
64 };
65 }
66 if (!fstat.isDirectory()) {
67 return null;
68 }
69 return _readdirRecursive({
70 path: file,
71 filter: options.filter,
72 });
73 });
74 })
75 .value();
76
77 // Note: we cannot flatten in the chain because we have an array of Promises
78 // which might themselves resolve into an array.
79 return Promise.all(work).then(_.flatten);
80 })
81 .then(function(results) {
82 // Special files were returned as null; cut them from results.
83 return _.reject(results, _.isNull);
84 });
85}

Related snippets