Skip to content

Instantly share code, notes, and snippets.

@itacirgabral
Created August 7, 2018 03:17
Show Gist options
  • Save itacirgabral/f001b52f6c384c5d22b61a4b6f968cf0 to your computer and use it in GitHub Desktop.
Save itacirgabral/f001b52f6c384c5d22b61a4b6f968cf0 to your computer and use it in GitHub Desktop.
generate csv with hash ,path, name from directory
const fs = require('fs')
const crypto = require('crypto')
const path2hash = p => crypto.createHash('sha256').update(p).digest('base64')
const basedir = process.argv.length > 2 ? process.argv[2] : process.env.PWD
const printout = x => process.stdout.write(x)
let csv = '\"hash\"\t\"path\"\t\"name\"'
printout(csv)
printout('\n')
const stopAt = Infinity
const dig = function dig (path, deep = 0) {
if (deep < stopAt) {
const here = fs.readdirSync(path)
const dirs = []
here.forEach(e => {
const twig = path + '/' + e
if (fs.lstatSync(twig).isFile()) {
printout('\"' + path2hash(twig) + '\"')
printout('\t ')
printout('\"' + path.slice(basedir.length) + '\"')
printout('\t ')
printout('\"' + e + '\"')
printout('\n')
} else {
dirs.push(e)
}
})
dirs.forEach(e => dig(path + '/' + e, deep + 1))
}
}
dig(basedir)
@itacirgabral
Copy link
Author

how to use

node folder2csv.js /path/to/directory > data.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment