Below is the exercise 5 of nodeschool learnyounode module
Create a program that prints a list of files in a given directory, filtered by he extension of the files. You w
Your problem is just a typo. You're doing this:
if(ext == ext){ // you're comparing the same variable
console.log(filename);
}
, but you should be doing this:
if(ext === ext1){ // try to use '==='
console.log(filename);
}
Other thing: they're not considering the .
of .txt
in the input, so you have to append this in your variable ext1
because .extname(file)
returns the extention with the .
:
var ext1 = '.' + process.argv[3];
The solution given uses the path module from Node JS package. The solution below doesn't use path, instead relies on simple deconstruction of given filename and using the parts needed.
-Import fs module
var fs = require('fs');
-Extract the path and ext name required from cmd line
let filePath = process.argv[2];
let extName = process.argv[3];
-Use (readdir) method to read the contents of a directory. The names of files inside the directory will be returned in the form of an array.
fs.readdir(filePath, 'utf-8', function(err, data) {
if (err) throw err;
data.forEach(element => {
-Take each element and split it into filename and extension name
let temp = element.split('.');
let tempSplit = temp[1];
if(tempSplit === extName) {
console.log(temp[0] + '.' + temp[1]);
}
});
Whole code for reference:
var fs = require('fs');
let filePath = process.argv[2];
let extName = process.argv[3];
fs.readdir(filePath, 'utf-8', function(err, data) {
if (err) throw err;
data.forEach(element => {
let temp = element.split('.');
let tempSplit = temp[1];
if(tempSplit === extName) {
console.log(temp[0] + '.' + temp[1]);
}
});
Heres what I came up with, if you want other solutions to problem:
var fs = require('fs');
var path = process.argv[2]; //first argument
var extension = process.argv[3]; //second argument
var re = new RegExp("."+extension, "g"); //a regexp that matches every string that begins with a dot and is followed by the extension, i.e. .txt
fs.readdir(path, function callback(err, list){ //read the directory
if (!err) { //if no errors occur run next funtion
list.forEach(function(val) { //take the list and check every value with the statement below
if(re.test(val)) { //if the .test() rexexp-function does not match it will return a false, if it does it will return true
console.log(val); //if it matches console log the value
}
});
}
});
You can try this code to solve this exercise :
var fs = require('fs');
function endsWith(str, suffix) {
var s = str.slice(str.length - suffix.length - 1);
if (s == "." + suffix)
return true;
else
return false;
};
fs.readdir(process.argv[2], function (err, list) {
if (process.argv[3]) {
for (var i = 0; i < list.length; i++) {
if (endsWith(list[i], process.argv[3]))
console.log(list[i]);
}
}
});
Here's the official solution:
var fs = require('fs')
var path = require('path')
fs.readdir(process.argv[2], function (err, list) {
list.forEach(function (file) {
if (path.extname(file) === '.' + process.argv[3])
console.log(file)
})
})
The only thing missing in your code is the concatenation of '.' before the file extension type.
var extension = '.'+ process.argv[3];
You can then do the comparison and printing.