Skip to content

Instantly share code, notes, and snippets.

@tylor
Created January 25, 2012 03:41
Show Gist options
  • Select an option

  • Save tylor/1674573 to your computer and use it in GitHub Desktop.

Select an option

Save tylor/1674573 to your computer and use it in GitHub Desktop.
Read all CSVs in a directory and do something with them
{
"author": "tylor",
"name": "csv_street_trees",
"version": "0.0.0",
"repository": {
"url": ""
},
"main": "trees.js",
"engines": {
"node": "~v0.4.9"
},
"dependencies": {
"csv": "0.0.10"
},
"devDependencies": {}
}
var csv = require('csv');
var fs = require('fs');
var filesFinished = 0;
var diameter = { DIAMETER: 0.00 };
var planted = { DATE_PLANTED: 2012 };
var total = 0;
function test(tree, index) {
total++;
if (tree.DIAMETER > diameter.DIAMETER) {
diameter = tree;
}
}
function done(count) {
filesFinished--;
if (filesFinished <= 0) {
console.log('Biggest: ' + diameter.DIAMETER);
console.log('Total trees: ' + total);
}
}
fs.readdir('./', function readFiles(err, files) {
var index = files.length;
while (index--) {
if (files[index].match(/(csv)$/i)) {
filesFinished++;
csv().fromPath(files[index], { columns: true })
.on('data', test)
.on('end', done);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment