Skip to content

Instantly share code, notes, and snippets.

@having-fun-coding
Created May 18, 2015 21:41
Show Gist options
  • Save having-fun-coding/429cf097bc11606eeda2 to your computer and use it in GitHub Desktop.
Save having-fun-coding/429cf097bc11606eeda2 to your computer and use it in GitHub Desktop.
Nodeserver | Cloud 9 | FILTERED LS
reate 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. Note that the second argument will not come prefixed with a '.'.
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 callback (err, list) { /* ... */ }
where list is an array of filename strings.
var fs = require('fs');
var path = require('path');
var mydir = process.argv[2];
var ext1 = '.' + process.argv[3];
fs.readdir(mydir, function(err, files){
if(err){
throw err;
}
//console.log(files);
files.forEach(function(filename){
var ext = path.extname(filename);
if(ext === ext1) {
console.log(filename);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment