FreeCodeCamp and Learning Node: Filtered LS

This one is similar to the previous two exercises except that instead of a file, we’re reading a directory’s contents.  So, we can use those as a jumping-off point.  The filter itself is simple as well – I didn’t call an outside function like I would with a program in the browser.  Hold that thought, though….we’ll see it again later!

As usual, if you haven’t at least attempted this one, GO TRY IT FIRST!  The description below makes it seem like I put this together easily and quickly, but truthfully, it took me awhile to get it, and there were a few false starts and rabbit trails.  So don’t get discouraged if you don’t get it right away!

For now, let’s take a look at the directions and hints for this exercise:

FILTERED LS (Exercise 5 of 13)

  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. Note that the second argument will not come prefixed with a ‘.’.
  Keep in mind that the first arguments of your program are not the first values of the process.argv array, as the first two values are reserved for system info by Node.
  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.

Documentation on the fs module can be found by pointing your browser here:

file:///home/ubuntu/.nvm/versions/node/v4.4.3/lib/node_modules/learnyounode/node_apidoc/fs.html

You may also find node’s path module helpful, particularly the extname method.

Documentation on the path module can be found by pointing your browser here:

file:///home/ubuntu/.nvm/versions/node/v4.4.3/lib/node_modules/learnyounode/node_apidoc/path.html

The documentation link, as I’ve mentioned before, is

https://nodejs.org/dist/latest-v6.x/docs/api/

So, we’re going to start this one the same as last time, with the require statement for fs.  Then, they got a little sneaky.  If you actually go to the documentation page for path, you’ll see that it needs its OWN require statement.  They don’t tell you that, so get in the habit of checking the documentation!

Next, process.argv[2] is … well, actually, here it’s a directory path, so I should have named it dirPath or something.  Anyway, I copied it from before.  They note above that element[3] in the arguments is the extension we’re filtering for, so I assigned that (process.argv[3]) to the variable extsn.  They also note that the extension will come without a “.” added to it, so I added that as well.  So far we have:

var fs = require('fs');
var path = require('path');
var filename = process.argv[2];
var extsn = "." + process.argv[3];

Okay, we’re done declaring variables.  On to the meat of the program!  We next add the fs.readdir() function with the same arguments we gave the fs.readFile() function before:

var bufFile = fs.readdir(filename, function callback(err, list) { 
});

As noted above, filename is really the directory path.

 

We need to make sure there’s no error – if(!err) – and then we need to loop through each of the files in the directory.  It’s not immediately clear, but the files are contained in ‘list’ in the fs.readdir() method above.  So, we loop through list.

path.extname() pulls off the the last three characters of the filepath and the dot (that’s why we needed to add a dot to extsn).  So, as we loop through each item in list, we’ll compare the item’s extension as returned by path.extname() to extsn and if they’re the same, we’ll write the file’s name to the console.

for (var item = 0; item < list.length; item++) {

 if (path.extname(list[item]) === extsn) {
 console.log(list[item]);
 }

 

And that completes the program!  Here’s the whole thing, for reference:

var fs = require('fs');
var path = require('path');
var filename = process.argv[2];
var extsn = "." + process.argv[3];

var bufFile = fs.readdir(filename, function callback(err, list) {
if(!err) {
for (var item = 0; item < list.length; item++) {

if (path.extname(list[item]) === extsn) {
console.log(list[item]);
}
}
}
});

See you next time!

Advertisements

One thought on “FreeCodeCamp and Learning Node: Filtered LS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s