Created
December 9, 2020 12:31
-
-
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
This file contains 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
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