Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Last active January 6, 2023 15:44
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 BananaAcid/627ba40667e8e4958b13162d9f1c15ff to your computer and use it in GitHub Desktop.
Save BananaAcid/627ba40667e8e4958b13162d9f1c15ff to your computer and use it in GitHub Desktop.
IN BROWSER: shows a directory picker, then gets all files from a local folder - How to
async function getDir() {
const dirHandle = await window.showDirectoryPicker({id: 'rememberMyLastSelected', mode: 'readwrite', 'startIn': 'documents'}); // https://developer.mozilla.org/en-US/docs/Web/API/Window/showDirectoryPicker#parameters
console.log('Opened file system: ', dirHandle);
// simple file list
for await (const entry of dirHandle.values()) {
console.log(entry.kind, entry.name);
}
// file list with details
const promises = [];
for await (const entry of dirHandle.values()) {
if (entry.kind !== 'file') {
continue;
}
promises.push(entry.getFile().then((file) => `${file.name} (${file.size})`));
}
console.log(await Promise.all(promises));
// create/open file and append string
const newFileHandle = await dirHandle.getFileHandle('repos.txt', { create: true });
console.log('created file', newFileHandle);
const file = await newFileHandle.getFile();
const contents = await file.text();
const writable = await newFileHandle.createWritable();
// Write the contents of the file to the stream.
await writable.write(contents + "Some text.\n");
// Close the file and write the contents to disk.
await writable.close();
}
getDir();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment