Skip to content

Instantly share code, notes, and snippets.

@josephrocca
Created December 9, 2020 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephrocca/86eb74805710854a9dfb0cc377944e87 to your computer and use it in GitHub Desktop.
Save josephrocca/86eb74805710854a9dfb0cc377944e87 to your computer and use it in GitHub Desktop.
Get all files within a directory (deep/recursive) using the web File System Access API
async function crawlDirectory(directoryHandle, files) {
for await (let [name, handle] of directoryHandle.entries()) {
if(handle.kind === "file") files.push(handle);
else if(handle.kind === "directory") crawlDirectory(handle, files);
}
}
let directoryHandle = await window.showDirectoryPicker();
let files = [];
await crawlDirectory(directoryHandle, files);
// Example to show how to use the file handles (we count hyphenated words):
let hyphenatedWordsHist = {};
for(let fileHandle of files) {
let file = await fileHandle.getFile();
let text = await file.text();
let hyphenatedWords = [...text.toLowerCase().matchAll(/\s([a-z]+-[a-z]+)[\s?!.]/g)].map(m => m[1]);
hyphenatedWords.forEach(w => hyphenatedWordsHist[w] = (hyphenatedWordsHist[w] || 0) + 1);
}
let top1000HyphenatedWords = Object.entries(hyphenatedWordsHist).sort((a,b) => b[1]-a[1]).slice(0, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment