Skip to content
murat aka edited this page Feb 6, 2018 · 1 revision

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/v6.11.2/lib/node_modules/learnyouno
de/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/v6.11.2/lib/node_modules/learnyouno
de/node_apidoc/path.html

─────────────────────────────────────────────────────────────────────────────

FILTERED LS (Exercise 5 of 13)

 //////////////////////////////////////////////
 /*  function exercise 5  FILTERED LS        */
 //////////////////////////////////////////////

 function filterLS(){

    var fs = require('fs'); // load file system module
    var pa = require('path'); // load path module
    var ex = process.argv[3]; // filename extension

    var fPath = process.argv[2]; // read file path from args

   fs.readdir(fPath, function doneReading(err, arrList) { // readFile is asycronous so a call back function is passed.

      if(!err){
     
         arrList.forEach(function (item){ // loop through filenames
     
           if (pa.extname(item) === '.' + ex) { // check if the filename extension is same as the passed in extension.
         
              console.log(item); // Print the filename.
         
           }
      
         });
     
      }

   });

 }

 filterLS();

Your submission results compared to the expected:

             ACTUAL                                 EXPECTED                

────────────────────────────────────────────────────────────────────────────────

"CHANGELOG.md" == "CHANGELOG.md"
"LICENCE.md" == "LICENCE.md"
"README.md" == "README.md"
"" == ""

────────────────────────────────────────────────────────────────────────────────

Submission results match expected

Used asynchronous method: fs.readdir()

PASS Your solution to FILTERED LS passed!

Here's the official solution in case you want to compare notes:

─────────────────────────────────────────────────────────────────────────────

var fs = require('fs')
var path = require('path')

var folder = process.argv[2]
var ext = '.' + process.argv[3]

fs.readdir(folder, function (err, files) {
  if (err) return console.error(err)
  files.forEach(function (file) {
    if (path.extname(file) === ext) {
      console.log(file)
    }
  })
})

─────────────────────────────────────────────────────────────────────────────

Clone this wiki locally