Created
January 25, 2012 03:41
-
-
Save tylor/1674573 to your computer and use it in GitHub Desktop.
Read all CSVs in a directory and do something with them
This file contains hidden or 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
| { | |
| "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": {} | |
| } |
This file contains hidden or 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 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