Created
August 29, 2015 21:26
-
-
Save JoeKarlsson/58e0db48fd3b6e3adc2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); // require is a special function provided by node | |
var path = require('path'); //require path function provided by node | |
var pathName = process.argv[2]; //full path name passed in as array | |
var ext = process.argv[3]; //extenstion we need to filter array by passed in as string | |
var filteredList = undefined; //Global variable. We do not know the file type, so I set to undefined. | |
function filteredLS(callback) { | |
fs.readdir(pathName, function doneReading(err, files) { //Async node call. Pass in pathname and async function | |
for (i = 0; i < files.length; i++) { //cycle through all of the filenames in the array | |
if (path.extname(files[i]) === '.' + ext) { //Check if the file name is equal to our extenstion filter with the node path.extname function | |
filteredList = files[i]; //If yes, set filtered list item to this value | |
callback(); //Call the async callback function | |
} | |
} | |
}) | |
} | |
function logMyAnswer() { //Callback function | |
console.log(filteredList); //console log the filtered item | |
} | |
filteredLS(logMyAnswer); | |
// Here's the official solution in case you want to compare notes: | |
// ──────────────────────────────────────────────────────────────────────────────── | |
// 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) | |
// }) | |
// }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment