Demo entry 6783755

getFilesUnderFolder

   

Submitted by jeff_terrell on Feb 20, 2019 at 15:08
Language: JavaScript. Code size: 543 Bytes.

const fs = require('fs')
const getFilesUnderFolder = dir => {
  const prefixed = entry => dir + '/' + entry
  const entries = fs.readdirSync(dir)
  const testEntry = statMethod => entry =>
        fs.statSync(prefixed(entry))[statMethod]()
  const subdirs = entries.filter(testEntry('isDirectory'))
  const files = entries.filter(testEntry('isFile'))
  const subResults = subdirs.map(sub => getFilesUnderFolder(prefixed(sub)))
  return [].concat.apply(files, subResults) // flattening concat
}

module.exports = getFilesUnderFolder

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).