-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilteredls.js
More file actions
47 lines (31 loc) · 1.24 KB
/
filteredls.js
File metadata and controls
47 lines (31 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
Create a program that prints a list of files in a given directory,
filtered by the extension of the files. You will be provided a
directory name as the first argument to your program (e.g.
'/path/to/dir/') and a file extension to filter by as the second
argument.
For example, if you get 'txt' as the second argument then you will
need to filter the list to only files that end with .txt.
The list of files should be printed to the console, one file per line.
You must use asynchronous I/O.
-----------------------------------------------------------------------
HINTS:
The `fs.readdir()` method takes a pathname as its first argument and a
callback as its second. The callback signature is:
function (err, list) { ... }
where `list` is an array of filename strings.
Documentation on the `fs` module can be found by pointing your browser
here:
/home/anca/lib/node_modules/learnyounode/node_apidoc/fs.html
You may also find the standard JavaScript `RegExp` class helpful here.
*/
var fs = require('fs');
var dirpath = process.argv[2];
var pattern = new RegExp("\\." + process.argv[3] + "$");
fs.readdir(dirpath, function(err, list) {
list.forEach(function(file) {
if (pattern.test(file)) {
console.log(file);
}
});
});