There are many ways to write this code but I can't understand why my version is not working. The program should take 2 arguments from the command line. The first is the path to a folder with files, and the second arg is a file extention. The output should print only the files with the same extention as the second argument. When I try to execute this code with the 2 arguments i get an empty output.
var fs=require('fs');
var path=require('path');
var filepath=process.argv[2];
var ext="."+process.argv[3];
fs.readdir(filepath,function(err,list){
if (err) throw err;
for (var i=0;i<list.length-1;i++){
if (("."+path.extname(list[i]))==ext){
console.log(list[i]);
}
}
});
Please, delete 'utf-8', you just need type (and read docs http://nodejs.org/api/fs.html#fs_fs_readdir_path_callback):
fs.readdir(filepath,function(err,list){